zoukankan      html  css  js  c++  java
  • 数组读取文件。

    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+"
    ");
                        }
                    } 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");然后来读取数组。

  • 相关阅读:
    Swift
    Swift
    POJ2029——Get Many Persimmon Trees
    windows-install-python-and-sphinx(*.rst file)
    【cocos2d-x】尝鲜 Cocos Code IDE(不断更新)
    mysql 删除重复数据sql声明
    开销是有益的:AppCan 至HTML5移动创新和创业精神和健康
    hibernate它5.many2one单向
    Insecure default in Elasticsearch enables remote code execution
    TestNg显示器(一个)-----监听器,类型和配置使用---另外META-INF详细解释
  • 原文地址:https://www.cnblogs.com/zlj843767688/p/10548777.html
Copyright © 2011-2022 走看看