0

I'm trying to do an assignment and I can't figure out why it won't write all of the collected data to a text file. Basically I need to read one .txt file called 'marks', output it (which it does) and then sort it into two files - if the grade is less than 50 it will go to fail.txt and if it is 50 or greater it will put it in the pass.txt. It only takes the first of the passes from the marks.txt and put it in pass.txt and one from the marks.txt I need all 8 to be sorted.

Here is the source:

import java.io.*;  

public class WriteKONG {  
public static BufferedReader read;  
public static PrintWriter WriteToPass;  
public static PrintWriter WriteToFail;  
public static String line;  


public static void main(String[] args) throws IOException  {  
        read = new BufferedReader(new FileReader("src/marks.txt"));  
        WriteToPass = new PrintWriter(new FileWriter("pass.txt"));  
        WriteToFail = new PrintWriter(new FileWriter("fail.txt"));  
        String StudentID;  
        String Course;  
        String MarkS;  
        int Mark;  

        line = read.readLine();  

        while(line != null)  
        {  
            sort();  
        }  


}  


 public static void sort() throws IOException  
{  

    read = new BufferedReader(new FileReader("src/marks.txt"));  
    WriteToPass = new PrintWriter(new FileWriter("pass.txt"));  
    WriteToFail = new PrintWriter(new FileWriter("fail.txt"));  
    String StudentID;  
    String Course;  
    String SMark;
    int Mark;  
    while (line != null) {
        line = read.readLine();  
        StudentID = line;  
        System.out.println("StudentID = " + StudentID);  

        line = read.readLine();  
        Course = line;  
        System.out.println("Course = " + Course);  

        line = read.readLine();
        SMark = line;  
        System.out.println("Mark = " + SMark + "\n");  

        Mark = Integer.valueOf(SMark);
        if(Mark >= 50)  
        {  
            WriteToPass.println(StudentID);  
            WriteToPass.println(Course);  
            WriteToPass.println(SMark);  
            WriteTopass.close();  
        }  

        else  
        {  
            WriteToFail.println(StudentID);  
            WriteToFail.println(Course);  
            WriteToFail.println(SMark);  
            WriteToFail.println(line);  
            WriteToFail.close();  
        }  

    }   

 } 
 }

Here is the marks.txt:

75676881
English
94
75676883
Math
78
75676885
Physics
24
75676887
Chemistry
89
75676889
English
35
75676891
History
24
4

2 回答 2

2

WriteToFail在第一次迭代后关闭并且永远不会关闭WriteToPass

您应该仅在循环后关闭这两个文件:

line = read.readLine();
while (line != null) {  
    StudentID = line;  
    System.out.println("StudentID = " + StudentID);  

    line = read.readLine();  
    Course = line;  
    System.out.println("Course = " + Course);  

    line = read.readLine();
    SMark = line;  
    System.out.println("Mark = " + SMark + "\n");  

    Mark = Integer.valueOf(SMark);
    if(Mark >= 50)  
    {  
        WriteToPass.println(StudentID);  
        WriteToPass.println(Course);  
        WriteToPass.println(SMark);  
    }  
    else  
    {  
        WriteToFail.println(StudentID);  
        WriteToFail.println(Course);  
        WriteToFail.println(SMark);  
        WriteToFail.println(line);  
    }  
    line = read.readLine();
}   
WriteToPass.close();  
WriteToFail.close();  

我刚刚注意到您在 main 方法和 sort 方法中打开文件。我认为没有理由这样做。您的 main 应该只调用 sort :

public static void main(String[] args) throws IOException  
{  
    sort();
}
于 2014-11-14T18:40:05.260 回答
0

尝试

WriteToPass = new PrintWriter(new FileWriter("pass.txt",true));
于 2014-11-14T18:55:03.200 回答