2 Replies Latest reply: May 4, 2010 3:06 PM by 800560 RSS

    A question about java.io.BufferedWriter/BufferedReader

    843853
      Is there a way to make BufferedWriter NOT erase
        • 1. Re: A question about java.io.BufferedWriter/BufferedReader
          843853
          Sorry for not finishing my question in the previous post.
          -----
          I am writing a program that will randomly assign People at a school setting to a table, but not to the same table more than 4 times over the course of the school year.
          I have three sets of text files:
          1) A single file for all of the possible tables a given person could
          sit at any time, along with how many open seats there are.
          2) One set for each grade: Freshmen, Sophomores, Juniors, and Senior (which will be updated yearly by adding new students and removing old ones).
          3) Finally, one set of files for each table host, that will contain all of the student who have previously sat at that table.
          What I have the program doing currently is reading in first two of the above sets of files, and randomly assigning them to a table. I then want the program to print each person's name as well as the
          String " x "
          and the number of times that that they have sat at that table.
          Example 1:
          The program is being run for the first time. Kelly has been assigned to Elizabeth's table, and Peter has been assigned to Alex's table. The
          "Kelly x 1"
          is printed into the file "Elizabeth.txt", while
          "Peter x 1"
          is written into "Alex.txt". This works fine the first time that you run the program.
          Example 2:
          The program is being run for the second time. Now Kelly is assigned to Martha's Table, while Peter is assigned to Michele's table. Here is the problem. Because there were other people at Martha's table BEFORE Kelly was assigned there, and likewise with Peter and Michele, the program updates the string to "Peter x 2". This is inaccurate, as Peter has never sat with Michele before, nor has Kelly with Martha. I believe it does this because the file previously read
          Daisy x 1
          Chelsea x 1
          JT x 1
          Robel x 1
          so it adds a line so the file now looks like this:
          Daisy x 1
          Chelsea x 1
          JT x 1
          Robel x 1
          Chelsea x 1
          Kelly x 1
          Example 3:
          The program is being run for the third time. Kelly has been reassigned to Elizabeth's table, and Peter has been reassigned to Alex's. The file now looks like this:
          Peter x 1
          ...
          ...
          ...
          Peter x 2
          ...
          ...
          ...
          Is this BufferedWriter's fault, or something else:
          String encoding = "UTF-8";
          BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToFiles2 + "temp.txt", true), encoding));
          Please let me know!!!
          Thanks!
          -Matthew

          Edited by: matthewd1992 on May 4, 2010 1:59 PM
          • 2. Re: A question about java.io.BufferedWriter/BufferedReader
            800560
            matthewd1992 wrote:
            Is this BufferedWriter's fault, or something else:
            String encoding = "UTF-8";
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(pathToFiles2 + "temp.txt", true), encoding));
            I am not sure I understand everything you are doing but one thing that seems to be causing some problems is the way you are creating the FileOutputStream. You are appending to the file, meaning you are adding to the end of it. You do not overwrite/erase anything in this case.

            I think your logic should be to read everything into data structures of some sort, process them, then when you write them out you should erase the old data.