使用Java中的ArrayList从文件中读取

时间:2015-04-17 04:45:06

标签: java arraylist writer

我有一个让我从文件中读取的作业,我们使用ArrayList来组织和声明数字,然后计算这些数字的平均值并将它们打印在一个新文件中。我知道我需要3个部分,这将是Reader,Writer和Array List,但是当我尝试从scaner读取时编译时出错。有人可以帮助解决如何使用ArrayList读取文件,以及如何写入新文件。

import java.util.ArrayList;
import java.util.Collections;
import java.io.*; //Replaces the scanner
import java.io.BufferedReader; 
import java.util.Scanner;
import java.io.FileReader; // Used by the BufferedReader import java.util.Scanner;
import java.io.FileNotFoundException; //
import java.io.IOException; //

    class SD9 {
         public static void main( String[] args ) {  
           try{
             FileReader Fr = new FileReader( "Patriots.txt" ); 
              // the file reader bridges the program and the .txt file together. 
              BufferedReader Br = new BufferedReader( Fr );
              String line = Br.readLine();
              // BufferredReaders can only read one line at a time.
               FileWriter fw = new FileWriter( "PatriotsStat.txt" );
               BufferedWriter bw = new BufferedWriter( fw );
                while( line != null ) {
              //BufferredReaders return null once they've reached     the end of the file.
                ArrayList<Double> Patriots = new ArrayList<Double>();
            for(int i = 0; i < 23; ++i ) {
                  Patriots.add( scan.nextDouble() ); 
                        }       

                 /* String Line1 = "2014 PreSeason:";
                 bw.write(  " "  );
                 bw.newLine();
                  /*String Line3 = " FinalAvg: " + finalAvg;
                  bw.write( Line3 );
                  bw.newLine();*/

               }
                              bw.close();
                   }catch( FileNotFoundException F ) {
               //.....
              }   catch( IOException I ) {
           }

        }
        }

3 个答案:

答案 0 :(得分:1)

这应该有效:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

class SD9 {
    public static void main(String[] args) throws FileNotFoundException {

        Scanner scanner = new Scanner(new File("Patriots.txt"));
        PrintWriter writer = new PrintWriter(new File("PatriotsStat.txt"));
        ArrayList<Double> Patriots = new ArrayList<Double>();
        double sum = 0;
        while (scanner.hasNext()) {
            double num = scanner.nextDouble();
            sum += num;
            Patriots.add(num);
        }
        scanner.close();
        for (int i = 0; i < Patriots.size(); i++) {
            writer.write(Patriots.get(i)+"\n");
        }
        double average = sum / Patriots.size();

        writer.write("Average : "+average);
        writer.close();
    }
}

答案 1 :(得分:0)

我相信这会有所帮助。

File fr = new File("Patriots.txt");
Scanner sc = new Scanner(fr); 
ArrayList<Double> patriots = new ArrayList<Double>(); 
while(sc.hasNextDouble()){
    patriots.add(sc.nextDouble);
}
sc.close();

答案 2 :(得分:0)

代码无法编译,因为您的代码不遵循try...catch块的正确Java语法

在Java中,try ... catch块遵循以下形式:

try {

    // do something...

} catch (Exception e) {

    // handle the exception...

}

在您的代码中,您会看到代码位于try区块和catch区块之间:

bw.close()行是罪魁祸首。

try {
    // code
}

bw.close();

} catch( FileNotFoundException F ) {
    //.....
} catch( IOException I ) {

}

假设您在IDE(NetBeans,Eclipse等)中执行此操作,可以在“构建输出”中找到此相关信息。窗口