java - 在 Java 中使用 ArrayList 从文件中读取

标签 java arraylist writer

我有一个作业要求我从文件中读取数据,使用 ArrayList 来组织和声明数字,然后计算这些数字的平均值并将其打印在新文件中。我知道我需要 3 个部分,即读取器、写入器和数组列表,但是当我尝试从扫描仪读取数据时,我在编译时收到错误。有人可以帮助如何使用 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 ) {
           }

        }
        }

最佳答案

这应该有效:

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();
    }
}

关于java - 在 Java 中使用 ArrayList 从文件中读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29690518/

相关文章:

java - 如果属性本身为空,则过滤掉列表数据

java - Hibernate,Spring框架和多对多

Java向数组列表添加未知项

java - 将对象以 json 格式写入 zip 文件

java - UIautomator - Android Studio - 如何生成 APK 或 JAR 文件

java - 获取 java.lang.IllegalStateException : Could not execute method of the activity after adding BigDecimal varibles.

java - 如何在 Java 的 ArrayList 类中使用 add()

java - 如何修改ListView的ArrayList,但ListView和ArrayList在不同的Activity中

java - 输入/输出流和普通的读写器有什么区别,它们的类型有什么区别?

python - Csv Writer - 尝试在每列中写入变量