Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-26-2008, 07:47 PM
Member
 
Join Date: Jun 2008
Posts: 3
p900128 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-26-2008, 08:53 PM
Senior Member
 
Join Date: Jun 2008
Posts: 317
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-26-2008, 09:04 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
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
Code:
60 Sally 95 Jill 91 Tom 87 Mark 75 Bill 60
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-26-2008, 11:32 PM
Member
 
Join Date: Jun 2008
Posts: 3
p900128 is on a distinguished road
I tried both of your suggestions but neither of them actually changed the file. Is there no way to rewrite the file itself?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-27-2008, 01:15 AM
Senior Member
 
Join Date: Jun 2008
Posts: 317
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
DES algorithm (Read and Write bytes to file) JoaoPe Advanced Java 6 07-29-2008 04:46 PM
Read/Find Substring/Write to new file hiklior New To Java 6 04-23-2008 03:47 AM
Read-File Write Display substring hiklior New To Java 3 04-18-2008 12:45 PM
How to read a text file from a Java Archive File Java Tip Java Tips 0 02-08-2008 10:13 AM
.jnlp cant read & write on browser despite signed .jar bongia New To Java 0 11-14-2007 07:04 PM


All times are GMT +3. The time now is 07:11 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org