第三周-数组-续

继上周的数组又提的进一步要求:     

      1.要求数组从文件读取。
      2.如果输入的数组很大,  并且有很多大的数字,  就会产生比较大的结果 (考虑一下数的溢出), 请保证你的程序能正常输出。
      3.另外, 如果输入文件的参数有错误, 这个程序应该能正常退出, 并显示相应的错误信息。 任何输入错误都不能导致你的程序崩溃。

思路:

1.向文件输入时采取一行一个数的方式输入。

2.采用容器list从文件中获取数据。解决数组容量问题

3.采用BigInteger解决数的溢出问题。

  1 package shuzuxu;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.FileReader;
  8 import java.io.FileWriter;
  9 import java.io.IOException;
 10 import java.io.InputStreamReader;
 11 import java.io.OutputStream;
 12 import java.math.BigInteger;
 13 import java.util.ArrayList;
 14 import java.util.List;
 15 import java.util.Scanner;
 16 import java.util.*;
 17 public class shuzu {
 18     public static void main(String[] args) throws Exception{
 19                 File f = new File("hello.txt");
 20                 judeFileExists(f);//判断文件是否存在
 21                 //用FileOutputSteam包装文件,并设置文件可追加
 22                 OutputStream out = new FileOutputStream(f,true);
 23                 Scanner in=new Scanner(System.in);
 24                 System.out.println("请输入产生随机数的数量:");
 25                 String nums;
 26                 int n;
 27                  while(true)//判断输入是否为整数
 28                 {
 29                       try
 30                       {
 31                        nums=in.next() ;
 32                        n=Integer.parseInt(nums) ;
 33                                break ;
 34                       }
 35                       catch(Exception e)
 36                       {
 37                        System.out.println("只能输入整数") ;
 38                       }
 39                 }
 40                 for(int i=0;i<n;i++)
 41                 {
 42                     java.util.Random r=new java.util.Random();
 43                     String s=Integer.toString(r.nextInt());
 44                     out.write(s.getBytes()); //向文件中写入数据
 45                     out.write('\r'); // \r\n表示换行
 46                     out.write('\n'); 
 47                 }
 48                 List list = new ArrayList();
 49                 BufferedReader bw = new BufferedReader(new FileReader(f));
 50                  String line = null;
 51                    //因为不知道有几行数据,所以先存入list集合中
 52                    while((line = bw.readLine()) != null){
 53                        list.add(line);
 54                    }
 55                    //bw.close();
 56                    int a=list.size();
 57                     String[] li=new String[a];
 58                    for(int i=0;i<list.size();i++)
 59                    {
 60                        li[i]=(String) list.get(i);
 61                        System.out.println(li[i]);
 62                    }
 63                    BigInteger maxsum = new BigInteger("0");
 64                    BigInteger maxstart = new BigInteger("0");//用于判断子数组是否小于0
 65                    BigInteger x = new BigInteger("0");
 66                     maxsum = new BigInteger(li[0]);
 67                     BigInteger y;
 68                     for(int i = 0;i < list.size();i++)
 69                     {
 70                         if (maxstart.compareTo(x)!=1) {//忽略掉和为负数和0的子数组
 71                             maxstart = new BigInteger(li[i]);
 72                         }else {
 73                             y= new BigInteger(li[i]);
 74                             BigInteger v=maxstart.add(y);
 75                             maxstart=v;
 76                         }
 77                         
 78                         if (maxsum.compareTo(maxstart)<0) {
 79                             maxsum = maxstart;
 80                         }
 81                     }
 82                     System.out.println("最大值为:" + maxsum);
 83                     FileWriter fileWriter =new FileWriter(f);
 84                     fileWriter.flush();
 85                     fileWriter.close();
 86                     String line1 = null;
 87                        if((line1 = bw.readLine()) == null){
 88                            System.out.println("文件清空成功");;
 89                        }
 90                     
 91             }
 92     public static void judeFileExists(File file) {//判断文件是否存在
 93         
 94                 if (file.exists()) {
 95                      System.out.println("file exists");
 96                  } else {
 97                    System.out.println("file not exists, create it ...");
 98                      try {
 99                          file.createNewFile();
100                      } catch (IOException e) {
101                          // TODO Auto-generated catch block
102                          e.printStackTrace();
103                      }
104                  }
105          }
106 }
shuzu

猜你喜欢

转载自www.cnblogs.com/yeshenfeng/p/10549634.html