Let's say I have a map.txt file, and I know what row and column the char I want to change is at. How do I do this?
Thank you for your time.
--- Update ---
Bump, please help me. This is the most crucial part of my game!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Let's say I have a map.txt file, and I know what row and column the char I want to change is at. How do I do this?
Thank you for your time.
--- Update ---
Bump, please help me. This is the most crucial part of my game!
Read all the file into memory, make the changes, write the file back to disk.
If you know the exact location (the displacement from the first byte of the file) of the characters in the file you could use the RandomAccessFile class to overwrite the data, if the new data has the same number of characters as the old data.
BTW Disk files do not have rows and columns, they contain bytes from 0 to the length-1.
If you don't understand my answer, don't ignore it, ask a question.
There's no easy/clean way to do this. It also depends on if you know how many characters are in each line and if this number is the same for all lines. And also if the charset of the file is a single-byte, fixed multi-byte or a variable multi-byte. Without these informations it's quite impossible to think a "random" access to modify a single character.
Other solutions:
1) Read all the content in memory (if possible), change in memory and then rewrite the entire file.
or
2) Read the file in small pieces by time, 1 char by time or 1 line by time and write to a new file changing "on the fly" what you need.
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
How can I rewrite everythimg back where the txt file wpuld still contain the old data. How do I delete everything inside it?
Writing to a file will replace ALL of its old contents if the write is NOT in append mode.
Write a small test program that reads a small file, changes it and writes it out. Look at what was written.
If you don't understand my answer, don't ignore it, ask a question.
sakonpure6 (December 22nd, 2013)