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 16 of 16

Thread: Writing to a txt file from the top.

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

    Default Writing to a txt file from the top.

    Let's say I have a text file with multiple lines of text. When I read the file and want to write to it, how can I add my text to the top of the file, rather than the bottom (after the original lines of text) ?

    This is a system I am trying to create to update my "Map.txt" file after some one picks up a coin , replace the coin with grass.

    if(m.getMap(p.getTileX(), p .getTileY() -1) . equals ("c")){
     
    						coinCounter ++;
     
    						//Read files to change coin position to grass
    						try{
    							r = new Scanner(new File("res/Map.txt"));
    							}catch (Exception e1){ System.out.println("Error Loading Map.txt File."); }
     
    						while(r.hasNext()){
     
    							for(int i = 0; i < 16; i++){
    									level[i] = r.next();
    							}
    						}
     
    						level [p.getTileY()].substring(p.getTileX() - 1 , p.getTileX()).replace ("c" , "g");
     
    						r.close();
     
    						//Writing back new correct data
     
    						try{
    						FileWriter fw = new FileWriter("res/Map.txt");
    						PrintWriter pw = new PrintWriter(fw);
     
    						pw.println();
     
    						for(int i = 0; i < 16 ; i++){
    							pw.println(level[i]);
    						}
     
    						}catch (Exception error){}
     
     
     
    					 pw.close();
    					}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a txt file from the top.

    how can I add my text to the top of the file
    Sort of like when you have a pile of books and you want to add some more books at the bottom of the pile.
    All the books must be picked up and the new books moved into place and the old books then placed on top.

    With the contents of a file, all the existing lines must be copied and saved, the new lines written and the old lines written after the new ones.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Writing to a txt file from the top.

    Quote Originally Posted by Norm View Post
    Sort of like when you have a pile of books and you want to add some more books at the bottom of the pile.
    All the books must be picked up and the new books moved into place and the old books then placed on top.

    With the contents of a file, all the existing lines must be copied and saved, the new lines written and the old lines written after the new ones.
    Great so lets say I copied all the old code, how do I get rid of the new one in the Map.txt file?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a txt file from the top.

    how do I get rid of the new one in the Map.txt file?
    I'm not sure what you are asking.
    Don't copy to the file, what you don't want in the file.

    Say the file has: ABC and you want to insert DE at the front to get: DEABC
    can you explain what you want with that example?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Writing to a txt file from the top.

    My "Map.tx" has the following:

    (Each Line 22 char long)
    tttttttttttttttttttttt
    tggggggggggggggggggggt
    tggggcgggggggggggggggt
    tgggggggggggggggcggggt
    tgggggggggugggggggcggt
    tgggyyyyyguggggggggggt
    tgggygcgyggugggggugggt
    tgggygggyggggugggugggt
    tgggygggygggcggugugggt
    tgggygggyggggggggugggt
    tgggygggyggggggugggugt
    tgygggggggygggguuuuugt
    tggyyyyyyygggggugggugt
    tggggggggggggggugggugt
    tgggggcgggggcggggggggt
    tttttttttttttttttttttt
    and you see where there is a "c", in my code if my char touches it, I want to replace it with a "g" which stands for grass. Therefore making the coin disappear. Now I think I have accomplished that with my code, but the problem now is to get Java to read my new Map design. Hence, I am trying to write the new Map lines to the same file but starting from the top. ( I have a loop in my project that reads the first 16 lines from Map.txt file, so the rest will be ignored)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a txt file from the top.

    Please make a simpler example to work with. That post has way too many letters in it.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Writing to a txt file from the top.

    okay for example :
    ttttcgggttttt
    how would you do it with this ?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a txt file from the top.

    how would you do it with this
    What do you want the file to look like after it is updated given the contents shown as in post#7 when the update starts?

    Basically, read in the file, update it in memory and write out the new version.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Writing to a txt file from the top.

    In accordance with post #7 , I want this to be written in the same file.
    ttttggggttttt

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a txt file from the top.

    Is this the update to the file: change a "c" to a "g"?
    Read the file into memory, change the "c" to a "g" and write the updated data back to the file.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Writing to a txt file from the top.

    exactly.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a txt file from the top.

    OK, that's should be an easy thing. Have you tried it? What problems are you having?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Writing to a txt file from the top.

    I know how to read entire lines of text from , column by column (is there a way to copy the text char by char?) . But I do not know how to delete all the useless data in the same .txt file.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Writing to a txt file from the top.

    (is there a way to copy the text char by char?)
    To copy the characters of a String you could use the charAt() method to get each char and save them in a StringBuilder class object. The StringBuilder class has methods for adding char and getting a String.

    know how to delete all the useless data in the same .txt file.
    Read the whole file into memory, remove the useless data and write out the whole file completely replacing what was in the file.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Writing to a txt file from the top.

    What useless data?

    Your Map file appears to be of a well-defined structure that could be read into a 2D array and manipulated as needed and then written to the file when necessary.

    I've gotten the impression, perhaps incorrectly, that your design requires frequent reading/writing to the file while the game is being played, perhaps in response to each move by the user. If that's the case, don't do that. Read the game board from the file when the game begins, play the game on the game board stored in memory, then write the game board to the file when the game is over. There's no reason for frequent file reading and writing that I can think of. If you think differently, please explain why it's necessary.

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

    Default Re: Writing to a txt file from the top.

    Quote Originally Posted by GregBrannon View Post
    What useless data?

    Your Map file appears to be of a well-defined structure that could be read into a 2D array and manipulated as needed and then written to the file when necessary.

    I've gotten the impression, perhaps incorrectly, that your design requires frequent reading/writing to the file while the game is being played, perhaps in response to each move by the user. If that's the case, don't do that. Read the game board from the file when the game begins, play the game on the game board stored in memory, then write the game board to the file when the game is over. There's no reason for frequent file reading and writing that I can think of. If you think differently, please explain why it's necessary.
    In my case , I have coins that the user collects. I want to be able to, when the user collects a coin, it's place to be replaced by a grass sprite. This is represented by the char "g" in the map .txt file.

Similar Threads

  1. Advice on removing duplicates from a .txt file while writing to an array
    By shakesgar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2013, 03:09 PM
  2. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  3. FileWrite Over-Writing txt Files. D:
    By Java Programmer in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 8th, 2012, 04:31 PM
  4. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  5. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM