Archived

This topic is now archived and is closed to further replies.

role

[java] game file

Recommended Posts

role    187
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

Share this post


Link to post
Share on other sites
aidan_walsh    739
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

Share this post


Link to post
Share on other sites
CaptainJester    523
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]

Share this post


Link to post
Share on other sites
role    187
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]

Share this post


Link to post
Share on other sites
Argus    118
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?

Share this post


Link to post
Share on other sites