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.

Results 1 to 6 of 6

Thread: Editing a Text File in Java, a specific part.

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Editing a Text File in Java, a specific part.

    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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Editing a Text File in Java, a specific part.

    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.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Editing a Text File in Java, a specific part.

    Quote Originally Posted by sakonpure6 View Post
    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?
    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

  4. #4
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Editing a Text File in Java, a specific part.

    How can I rewrite everythimg back where the txt file wpuld still contain the old data. How do I delete everything inside it?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,408
    Thanks
    49
    Thanked 2,214 Times in 2,187 Posts

    Default Re: Editing a Text File in Java, a specific part.

    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.

  6. The Following User Says Thank You to Norm For This Useful Post:

    sakonpure6 (December 22nd, 2013)

  7. #6
    Member
    Join Date
    Sep 2013
    Posts
    41
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Editing a Text File in Java, a specific part.

    Quote Originally Posted by Norm View Post
    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.
    Ohhhh , finally thank you for this reply.

Similar Threads

  1. Replies: 3
    Last Post: December 13th, 2013, 11:19 AM
  2. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  3. Writing to a specific line in a text file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 7th, 2011, 08:11 PM
  4. How to write to specific part of an html file using java
    By nasi in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: May 27th, 2010, 11:22 PM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM