Skip to main content
gamedev.net
🔒 Locked

[java] game file

Started by role Apr 30, 2003 at 1:13 PM 5 replies 600+ views
Original Post
role
role
i got trouble in reading/writing my game state into file this is all about reading/writing STRING object into file first, can i add String into the end of file? (without have to rewrite all from the beginning) i use FileWriter, BufferedWriter, and PrintWriter to do file writing. like this :
	private void fileWrite(String[] data,File fileName) {
	try {
		FileWriter out = new FileWriter(fileName);
		BufferedWriter writeOut = new BufferedWriter(out);
		PrintWriter fos = new PrintWriter(writeOut);

		fos.println(data.length);
		for (int i=0;i < data.length;i++)
			fos.println(data);
		fos.close();
	} catch (IOException e) { e.printStackTrace(); }
	}
 
the String[] data is not append to the end of file, but make a new file. second, how to read n line of file. example i want to read line 27 of my file to line 36 and replace that lines, then write it to the same file again. thanxx anyone
aidan_walsh
aidan_walsh
If you loaded the former file into a StringBuffer first, appended your new info onto the end of that, and then rewrote the whole thing (I know you said you didn''t want to do that...) it should solve your difficulties.


SketchSoft OFFLINE | SketchNews

www.aidanwalsh(.net)(.info)
CaptainJester
CaptainJester
From the Java API docs:

java.io.RandomAccessFile

Instances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek method.





First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

[edited by - CaptainJester on May 1, 2003 7:21:12 AM]
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
fractionMan
fractionMan
quote:

for (int i=0;i < data.length;i++) fos.println(data);


also, shouldn''t that be:
fos.println(data[i]);
role
role
fractionMan yup that's "i" in fos.println(data-i-);
but the messageboard make it italic
doodle_sketch maybe that would be the last idea i will do if there's no other way
is there any class to manage this io file, special for String?
CaptainJester, java.io.RandomAccessFile is for byte read/write isn't it? i know it can be use for String, but is there other alternatives?

[edited by - role on May 1, 2003 12:45:47 PM]
Argus
Argus
role - You can append to a file, and it''s pretty simple. Create your FileWriter with FileWriter(File file, boolean append) setting ''append'' to true. Use your Javadocs.

As to your second question, if you want fast access then you may want to think about hashing to the file, or maybe even using something like a B-tree to store the addresses of each String. But why not just store the Strings in memory?
role
role
okay thanx all
one task finished
now i can save my game file more efficiently
thanxx

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.