trama
trama

Reputation: 1401

How do I write back to a .txt file?

I am reading in a text file that has five pieces of information, one per line, for each object. With no line breaks. And with that information, I populate it in an ArrayList. From there I can modify it. Like add new objects, remove, and print back a group of objects. That methods seem to work file, because if I put in a line to print the ArrayList, it'll print the modified list.

I am having trouble saving the ArrayList into a new .txt file. A new file is created, but it's blank. I am not sure why because like I said, each time a select a method to modify my ArrayList, it returns that new list.

Method to read in .txt file.

public void loadData(File fileName) {

Scanner inputFile = null;

    try {
        inputFile = new Scanner (new File("textImport.txt"));
    }

    catch (Exception FileNotFoundException) {
        System.out.println("ERROR: " + FileNotFoundException.getMessage());
        System.exit(1);
    }

    while (inputFile.hasNext()) {
        String fileName = inputFile.nextLine();
        String fileGrade = inputFile.nextLine();
        String fileMajor = inputFile.nextLine();
        int fileYear = Integer.parseInt(inputFile.nextLine());
        double fileId = Double.parseDouble(inputFile.nextLine());
        Student stu = new Student (fileName , fileGrade , fileMajor , 
                fileYear , fileId );
        studentList.add(stu) ;
    }
}

My attempt to export the file.

public void save() {

    PrintWriter outputFile = null;
    try {
        outputFile = new PrintWriter("textExport.txt");
    } 

    catch (IOException e) {
        e.printStackTrace();
    }

    while (inputFile.hasNextLine()) {
        String line = inputFile.nextLine();
        outputFile.write(line);
        outputFile.close();
    }
}

Upvotes: 1

Views: 1700

Answers (2)

trama
trama

Reputation: 1401

With the help of Aubin's ideas, what worked for me:

public void save() {
   PrintWriter outputFile = null;
   try {
      outputFile = new PrintWriter("exportFile.txt");
      for ( Student stu : studentList) {
         outputFile.println(stu);
      }
   } 
   catch (IOException e) {
      e.printStackTrace();
   }
   if( outputFile != null ) [
      outputFile.close();
   }
}

Upvotes: 0

Aubin
Aubin

Reputation: 14853

The loop for saving must be built around your list, not the input file.

the file has to be entirely re-written, you can't append to it because you have delete operation.

public void save() {
   try( PrintWriter outputFile = new PrintWriter("textExport.txt")) {
      for( Student s : studentList ) {
         s.writeTo( outputFile );
      }
      outputFile.close(); // Optional, try() close it in any case.
   }
}

Class Student, method writeTo():

public void writeTo( PrintWriter outputFile ) throws IOException {
   outputFile.println( this.fileName );
   outputFile.println( this.fileGrade );
   outputFile.println( this.fileMajor );
   outputFile.println( this.fileYear );
   outputFile.println( this.fileId );
}

I suggest a static method into Student: (may be re-factored later into a Factory)

public static Student readFrom( Scanner source ) {
   this.fileName  = source.nextLine();
   this.fileGrade = source.nextLine();
   this.fileMajor = source.nextLine();
   this.fileYear  = Integer.parseInt( source.nextLine());
   this.fileId    = Double.parseDouble( source.nextLine());
   return new Student( fileName, fileGrade, fileMajor,  fileYear, fileId );
}

And the loader becomes:

public void loadData( File fileName ) {
   try( Scanner scanner = new Scanner( new File( "textImport.txt" ))) {
      while( scanner.hasNext()) {
            studentList.add( Student.readFrom( scanner ));
      }
   }
}

Upvotes: 1

Related Questions