Java不会将所有文件写入文件

时间:2014-11-14 18:38:47

标签: java readline readfile writefile

我正在尝试执行任务,但我无法弄清楚为什么它不会将所有收集的数据写入文本文件。基本上我需要读一个名为'标记'的文件,输出它(它做),然后将它分成两个文件 - 如果等级小于50,它将转到fail.txt,如果将它放在pass.txt中它是50或更高。它只接受来自marks.txt的第一个传递,并将它放在pass.txt中,而将一个来自marks.txt,我需要对所有8个进行排序。

以下是来源:

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

    }   

 } 
 }

这是marks.txt:

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

2 个答案:

答案 0 :(得分: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方法中打开文件。我认为没有理由这样做。你的主要应该叫sort:

public static void main(String[] args) throws IOException  
{  
    sort();
}

答案 1 :(得分:0)

尝试

WriteToPass = new PrintWriter(new FileWriter("pass.txt",true));