Results 1 to 5 of 5
  1. #1
    p900128 is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Default File read/write problems

    I want to read a file containing high scores then add a new score and then finally rewrite the file with the rearranged top five scores. I'm not getting any errors but when I look at the file nothing has changed.

    public synchronized void addHighScore(double s) {
    FileReader in;
    BufferedReader readFile;
    FileWriter out;
    BufferedWriter writeToFile;
    double lowest = 0;
    String previous[][] = new String[5][2];
    String newScores[][] = new String[5][2];
    try {
    in = new FileReader(dataFile);
    readFile = new BufferedReader(in);
    lowest = Double.parseDouble(readFile.readLine());
    for (int i=0; i<5; i++) {
    previous[i][0] = readFile.readLine();
    previous[i][1] = readFile.readLine();
    }
    for (int i=0; i<5; i++) {
    if (s>Double.parseDouble(previous[i][1])) {
    if (i==0) {
    continue;
    } else {
    newScores[i-1][0] = previous[i][0];
    newScores[i-1][1] = previous[i][1];
    if (i==1) {
    lowest = Double.parseDouble(newScores[i-1][1]);
    }
    }
    } else {
    newScores[i-1][0] = "newPlayer";
    newScores[i-1][1] = new Double(s).toString();
    if (i==1) {
    lowest = s;
    }
    for (int k=i; k<5; k++) {
    newScores[i][0] = previous[i][0];
    newScores[i][1] = previous[i][1];
    }
    break;
    }
    }
    in.close();
    readFile.close();
    } catch (IOException e) {
    System.out.println("Problem reading file.");
    System.err.println("IOException: " + e.getMessage());
    }
    try {
    out = new FileWriter(dataFile);
    writeToFile = new BufferedWriter(out);
    writeToFile.write(new Double(lowest).toString());
    writeToFile.newLine();
    for (int i=0; i<5; i++) {
    writeToFile.write(newScores[i][0]);
    writeToFile.newLine();
    writeToFile.write(newScores[i][1]);
    writeToFile.newLine();
    }
    out.close();
    writeToFile.close();
    } catch (IOException e) {
    System.out.println("Problem reading file.");
    System.err.println("IOException: " + e.getMessage());
    }
    }

    The first line of the file is the lowest score value so that the whole file doesn't need to be read to know whether or not this method needs to be run. After the first line there are five sets of 2 lines with a name on the first and a score on the second.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Close only the outer most Stream. In your case the BufferedReader and BufferedWriter.

    Do you know what a buffer is? It stores up data until it reaches a certain amount (or until you issue a certain directive). By closing your FileWriter without having flushed your BufferedWriter, a part of what you've attempted to write is still in the buffer (seemingly the exact part you want to add). Closing the BufferedWriter will automatically flush it (and, automatically, close all underlying streams), so close only the outermost Stream.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.io.*;
    
    public class RWTest {
        public static void main(String[] args) {
            RWTest test = new RWTest();
            boolean couldInsert = test.addHighScore("Carol", 90);
            System.out.println("couldInsert = " + couldInsert);
        }
    
        private static boolean addHighScore(String newPlayer, double s) {
            double lowest = 0;
            String[][] previous  = new String[5][2];
            String[][] newScores = new String[5][2];
            try {
                String dataFile = "rwTest.txt";
                FileReader in = new FileReader(dataFile);
                BufferedReader readFile = new BufferedReader(in);
                lowest = Double.parseDouble(readFile.readLine());
                if(s < lowest) {
                    readFile.close();
                    return false;
                }
                for (int i=0; i<5; i++) {
                    previous[i][0] = readFile.readLine();
                    previous[i][1] = readFile.readLine();
                }
                readFile.close();
            } catch (IOException e) {
                System.out.println("Problem reading file.");
                System.err.println("IOException: " + e.getMessage());
            }
            print(previous, "previous:");
            // Initialize newScores elements.
    
            for (int i=0; i<5; i++) {
                if (s < Double.parseDouble(previous[i][1])) {
                    newScores[i][0] = previous[i][0];
                    newScores[i][1] = previous[i][1];
                } else {
                    newScores[i][0] = newPlayer;
                    newScores[i][1] = Double.valueOf(s).toString();
                    for (int k=i+1; k<5; k++) {
                        newScores[k][0] = previous[k-1][0];
                        newScores[k][1] = previous[k-1][1];
                    }
                    break;
                }
            }
            lowest = Double.valueOf(newScores[newScores.length-1][1]).doubleValue();
            print(newScores, "newScores:");
    
            try {
                String outFilePath = "rwTestResult.txt";
                FileWriter out = new FileWriter(outFilePath);
                BufferedWriter writeToFile = new BufferedWriter(out);
                writeToFile.write(new Double(lowest).toString());
                writeToFile.newLine();
                for (int i=0; i<5; i++) {
                    writeToFile.write(newScores[i][0]);
                    writeToFile.newLine();
                    writeToFile.write(newScores[i][1]);
                    writeToFile.newLine();
                }
                writeToFile.close();
            } catch (IOException e) {
                System.out.println("Problem writing file.");
                System.err.println("IOException: " + e.getMessage());
            }
            return true;
        }
    
        private static void print(String[][] array, String s) {
            System.out.println("----- " + s + " -----");
            for(int i = 0; i < array.length; i++) {
                System.out.printf("%5s  %s%n", array[i][0], array[i][1]);
            }
            System.out.println("-------------------");
        }
    }
    rwTest.txt
    Java Code:
    60
    Sally
    95
    Jill
    91
    Tom
    87
    Mark
    75
    Bill
    60

  4. #4
    p900128 is offline Member
    Join Date
    Jun 2008
    Posts
    3
    Rep Power
    0

    Default

    I tried both of your suggestions but neither of them actually changed the file. Is there no way to rewrite the file itself?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    You are looking for the results in rwTestResult.txt and not rwTest.txt, right? As that is the file that you're writing. If you want the results to be in rwTest.txt, then, after writing the rwTestResult.txt rename rwTest.txt to something else and then rename rwTestResult.txt to rwTest.txt. Then, when those succeed, delete the original rwTest.txt.

Similar Threads

  1. DES algorithm (Read and Write bytes to file)
    By JoaoPe in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 03:46 PM
  2. Read/Find Substring/Write to new file
    By hiklior in forum New To Java
    Replies: 6
    Last Post: 04-23-2008, 02:47 AM
  3. Read-File Write Display substring
    By hiklior in forum New To Java
    Replies: 3
    Last Post: 04-18-2008, 11:45 AM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:13 AM
  5. Replies: 0
    Last Post: 11-14-2007, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •