没有输出到文件java

时间:2017-12-13 04:24:05

标签: java file

我试图从文件中读取答案密钥,并通过将其与最后一行进行比较并将其发送到文件来读取每一行。我尝试发送到文件,但没有输出,不知道这里有什么问题?
 在这里输入代码

`import java.util.Scanner;

import java.io. *; import java.io.PrintWriter;

public class tester3 {     public static void main(String [] args)抛出IOException {

    final int answer_size = 20;
    final int class_size = 20;

    final int answer_key_size = 20;

    String [][] answers = new String[answer_size][class_size];
    String [] correctAns = new String[20];

    readArray(answers);
    getAnswers(answers, correctAns);
    compareAnswers(answers, correctAns);


}

//read file ans into char array
public static void readArray(String[][] ar)throws IOException{
    File file = new File("answers.txt");
    Scanner inputFile = new Scanner(file);
    int index = 0;
    while(inputFile.hasNextInt() && index < ar.length){
    for(int row = 0; row < ar.length; row++){
        for(int col = 0; col < ar[row].length; col++){
            ar[row][col] = inputFile.nextLine();
            index++;
            //ar[row][col] = Character.toString(ar[row][col].charAt(row));
            System.out.println(ar[row][col]);
        }
    }
}

}

//read last row into 1d array
public static void getAnswers(String[][] ar, String [] ar2)throws IOException{
            File file = new File("answers.txt");
            Scanner inputFile = new Scanner(file);
            int index = 0;
            while(inputFile.hasNextInt() && index < ar.length){
            for(int row = 0; row < ar.length; row++){
                for(int col = 0; col < ar[row].length; col++){
                    //ar[row][col] = inputFile.nextLine().charAt(row);
                    if(row == 20){
                        inputFile.nextLine();
                        ar2[row] = Character.toString(ar[row][col].charAt(row));
                    }
                }
                index++;
            }
}

}
 //compare answers and send the results to an output file
public static void compareAnswers(String[][] ar, String [] ar2)throws IOException{
    PrintWriter writer = new PrintWriter("results.txt");
    File file = new File("answers.txt");
    int [] grades = new int[20];
                    Scanner inputFile = new Scanner(file);
                    int index = 0;
                    int count = 0;

                    writer.println("STUDENT EXAM RESULTS");
                    writer.println("Student number ----  # correct answers ---- Grade Received");

                    while(inputFile.hasNextInt() && index < ar.length){
                    for(int row = 0; row < ar.length; row++){
                        for(int col = 0; col < ar[row].length; col++){
                            ar[row][col] = Character.toString(ar[row][col].charAt(row));
                            if(ar[row][col].equals(ar2[row])){
                                count++;
                                grades[row] = (count/20) * 100;
                                writer.println(ar[row][col] + "----" + count + "----" + grades[row]);
                                writer.flush();
                            }
                        }
                        count = 0;
                        index++;
                    }
}

    writer.close();

}

}

1 个答案:

答案 0 :(得分:0)

如果要阅读文件的内容,例如TXT文件,则应使用BufferedReader。你可以这样做:

public ArrayList<String> getFileLines(String filePath) throws IOException {
    ArrayList<String> content = new ArrayList<>();
    String line = "";
    BufferedReader in = new BufferedReader(new FileReader(filePath));
    while((line=in.readLine()) != null) {
        content.add(line);
    }
    return content;
}

其中filePath可以是您要读取的文件的绝对路径,并将文件内容作为ArrayList字符串返回,并且易于处理。

然后,如果你想在文件上再写一些东西,例如TXT文件,你应该使用BufferedWriter

public void writeArrayInFile(String filePath, ArrayList<String> content) throws IOException {
    BufferedWriter out = new BufferedWriter( new FileWriter(filePath) );
    for (String l : content) {
        out.write(l + "\n");
    }
    out.close();
}

在这种情况下,filePath应该是您要创建的文件的绝对路径,但是如果文件已经存在则要小心,此方法将覆盖文件的内容。而ArrayList<String>只是您要写的内容。