数组读取文件。

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class test {
    public static void main(String[] args)throws IOException
    {
        List<Integer> list=new ArrayList<>();
        Scanner scan=new Scanner(System.in);

            FileWriter fw = null;
              try {
                    //创建字符输出流
                    fw = new FileWriter("D://test.txt");
                    for(int i=0;i<50;i++)
                    {
                        int intval=(int)(Math.random()*-9000000+8999999);
                    fw.write(""+intval+"\r\n");
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                } finally {
                    //使用finally块来关闭文件输出流
                    if (fw != null) {
                        fw.close();
                    }
                }
              try {
                    File file = new File("D://test.txt");
                    if(file.isFile() && file.exists()) {
                      InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "utf-8");
                      BufferedReader br = new BufferedReader(isr);
                      String lineTxt = "";
                      while ((lineTxt = br.readLine()) != null){
                       list.add(Integer.parseInt(lineTxt));
                      }
                      Object[]a=list.toArray();
                     
                      br.close();
                         int sum=a.length;
                         int sum1=0;
                            for(int i=1;i<=sum;i++)
                            {
                                sum1=sum1+i;
                            }
                            int [] b=new int[sum1];
                            int temp1=0;
                            for(int i=0;i<sum;i++)
                            {
                                int temp=0;
                                for(int k=0;k<sum-i;k++)
                                {   
                                    temp=temp+(int)a[i+k];
                                    b[temp1]=temp;
                                    temp1++;
                                }
                            }
                            for(int i=0;i<sum1-1;i++)
                                for(int k=0;k<sum1-1-i;k++)
                                {
                                    if(b[k]<b[k+1])
                                    {
                                        int temp2=b[k];
                                        b[k]=b[k+1];
                                        b[k+1]=temp2;
                                    }
                                }
                            System.out.println("最大的子数组和为"+b[0]);
                    } else {
                      System.out.println("文件不存在!");
                    }
                  } catch (Exception e) {
                    System.out.println("文件读取错误!");
                  }
        }
        
     
          

设计思想:首先先fw = new FileWriter("D://test.txt");创建字符输出流,int intval=(int)(Math.random()*-9000000+8999999);以此来创建随机数组,File file = new File("D://test.txt");然后来读取数组。

猜你喜欢

转载自www.cnblogs.com/zlj843767688/p/10548777.html
0条评论
添加一条新回复