Search code examples
javareadlinereadfilewritefile

Java wont Write all to file


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

Solution

  • You are closing WriteToFail after the first iteration and never close WriteToPass.

    You should close both files only after the loop:

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

    I just noticed that you open the files in the main method as well as in the sort method. I see no reason for doing that. Your main should just call sort :

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