Page 1 of 2 12 LastLast
Results 1 to 20 of 37
  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Editing TXT file?

    Hello once again everybody! I have a question on how do I modify txt file?
    My txt file named "user.txt" contains inside:
    Java Code:
    password bla
    iron 5
    gold 1005
    money 564
    What I need it to do is to edit the gold's value to 12 (the number can be string or integer it doesn't matter for me).
    Could you help me?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Editing TXT file?

    The steps involved include:
    1. Read in the text file. A Scanner could work well for this.
    2. Parse the text file as it is being read in. In other words translate the information held into useful information, into Java objects and data. In your situation this may be simple as finding the line that holds the gold value.
    3. Change the data. In your situation this may involve simply changing the data held by the line that holds the gold data.
    4. Write out the new text file, perhaps using a PrintWriter that holds a FileWriter.

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Fubarable View Post
    The steps involved include:
    1. Read in the text file. A Scanner could work well for this.
    2. Parse the text file as it is being read in. In other words translate the information held into useful information, into Java objects and data. In your situation this may be simple as finding the line that holds the gold value.
    3. Change the data. In your situation this may involve simply changing the data held by the line that holds the gold data.
    4. Write out the new text file, perhaps using a PrintWriter that holds a FileWriter.
    And how about deleting the file?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Editing TXT file?

    Quote Originally Posted by Lionlev View Post
    And how about deleting the file?
    My steps would have you over-writing the file. A safer way if desired would be to first copy the file before over-writing it.

  5. #5
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Fubarable View Post
    My steps would have you over-writing the file. A safer way if desired would be to first copy the file before over-writing it.
    Oh ok thanks

    I wrote some code and I am quite stuck...
    Java Code:
    String setCMD[] = new String[1];
    setCMD[0] = "gold"; //value that should be changed
    setCMD[1] = "12"; //into this
    try
    {
            checkUser4 = new Scanner(new File("user.txt"));
    } catch (Exception e) {}
    String fileContent = checkUser4.toString();
    fileContent.replaceAll(setCMD[0]+" ", setCMD[0]+ " " +setCMD[1]); // Can't seemed to figure it out how to find the number that comes after "gold"
    Can't seemed to figure it out how to find the number that comes after "gold"

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Editing TXT file?

    What does the posted code have to do with the problem of reading lines from a file? What is the setCMD array for?

    You need to read the API doc for the Scanner class to see what methods it has for reading data from a file.

    I suggest you break the problem up into several simple steps.
    Read and print the lines in the file.
    When that works, then search the lines as they are read for the one to be editted.
    When that works, change the contents of the line.

    Later worry about creating the new updated file, until later.

    Do the above steps one at a time. Get each to work before moving to the next.
    Last edited by Norm; 06-17-2012 at 07:36 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Editing TXT file?

    • Won't that code throw an array index out of bounds exception? You've declared the array to have a length of 1, and yet try to insert two items into it.
    • You don't want to ignore exceptions as you are doing.
    • Consider using a StringBuilder to hold the new file's text, and reading from the old file into the StringBuilder, unless it's then line you want to change, then insert your new text in its place.

  8. #8
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    What does the posted code have to do with the problem of reading lines from a file? What is the setCMD array for?

    You need to read the API doc for the Scanner class to see what methods it has for reading data from a file.

    I suggest you break the problem up into several simple steps.
    Read and print the lines in the file.
    When that works, then search the lines as they are read for the one to be editted.
    When that works, change the contents of the line.

    Later worry about creating the new updated file, until later.

    Do the above steps one at a time. Get each to work before moving to the next.
    I don't have problems with the code yet, I just don't know how to write a code that will find a line in a string that I need to edit

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Editing TXT file?

    Look at the String class's methods for ways to find one String in another String.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    Look at the String class's methods for ways to find one String in another String.
    Well there is some indexOf searches for the specific word, but I am not sure how to get the number that comes after it....
    Should I read the file spread everything in to arrays like name[] and number[] by using loop?
    int num = 0;
    while(string.hasNext){
    name[num] = string.next();
    string.next();
    num++;
    }

    Like this? Or there is a better way?

  11. #11
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    Look at the String class's methods for ways to find one String in another String.
    Forgot to mention... We don't know what the number of gold is. It's always "random"

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Editing TXT file?

    I would read each line, one at a time, and thus would use the Scanner's hasNextLine() and nextLine() methods. I would use one of String's methods to see if the String read in contains "gold" in it, and then act if it does.

  13. #13
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Fubarable View Post
    I would read each line, one at a time, and thus would use the Scanner's hasNextLine() and nextLine() methods. I would use one of String's methods to see if the String read in contains "gold" in it, and then act if it does.
    Is this the right way to do it?
    Java Code:
    //checkuser4 is a scanner
    
    while(checkUser4.hasNextLine()){
    	if(checkUser4.findInLine(setCMD[0]) == setCMD[0]){
    		String lol1 = checkUser4.nextLine();
    		splitU = lol1.split(" ");
    	}
    }
    So I get the splitU[0] = gold and split[1] = the gold's value

    AHHH NVM CHANGED THE CODE TO SOMETHING ELSE.....
    Last edited by Lionlev; 06-17-2012 at 10:54 PM.

  14. #14
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    Look at the String class's methods for ways to find one String in another String.
    Ok I wrote this code, but it gives me error "No lines found":
    Java Code:
    try
    {
    	checkUser4 = new Scanner(new File("user.txt"));
    } catch (Exception e) {}
    
    String splitU[] = new String[1];
    												
    while(checkUser4.hasNextLine()){
    	String lol1 = checkUser4.nextLine();
    	if(lol1.contains("gold")){
            	splitU = lol1.split(" ");
    		System.out.println("1st: "+splitU[0] + "Second: " +splitU[1]);
    	} else {
    		checkUser4.nextLine();
    	}
    }

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default Re: Editing TXT file?

    it gives me error "No lines found":
    When you get error messages, please copy the full text and paste them here.


    You should not ignore exceptions. Add a call to printStackTrace() to the empty catch block.


    I still suggest that you write the code to do one thing at a time. Start with a simple loop to read all the lines in the file and print them, nothing else. Just read and print the lines.
    Last edited by Norm; 06-17-2012 at 11:06 PM.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    When you get error messages, please copy the full text and paste them here.


    You should not ignore exceptions. Add a call to printStackTrace() to the empty catch block.
    The error : java.util.NoSuchElementException: No line found

    The txt file:
    password f6
    ulvl 6
    uexp 500
    gold 100
    uiron 20
    ucrystal 5
    coal 2
    ulayer 8
    umaxlayer 11

  17. #17
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    When you get error messages, please copy the full text and paste them here.


    You should not ignore exceptions. Add a call to printStackTrace() to the empty catch block.


    I still suggest that you write the code to do one thing at a time. Start with a simple loop to read all the lines in the file and print them, nothing else. Just read and print the lines.
    Ok, I am gonna print everything out ...

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Editing TXT file?

    If you have a while block that checks a Scanner for hasNextLine(), it is key that you call nextLine on the Scanner object only once within that same block. If you don't follow this rule, then you're doing an unchecked read on the Scanner and are asking for trouble. The same is true for any of the Scanner#hasNextXXX()/Scanner#nextXXX() method pairs.

  19. #19
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Norm View Post
    When you get error messages, please copy the full text and paste them here.


    You should not ignore exceptions. Add a call to printStackTrace() to the empty catch block.


    I still suggest that you write the code to do one thing at a time. Start with a simple loop to read all the lines in the file and print them, nothing else. Just read and print the lines.
    OBBAAA Got it to work, thanks for the tip!
    Now all I have to do is to convert the whole text file in to a string so I can modify it, how can I do that? IS there anything like string = scanner.readAll();
    ?

    Oh and yeah I want to change the line in the text file
    Last edited by Lionlev; 06-17-2012 at 11:39 PM.

  20. #20
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Editing TXT file?

    Quote Originally Posted by Fubarable View Post
    If you have a while block that checks a Scanner for hasNextLine(), it is key that you call nextLine on the Scanner object only once within that same block. If you don't follow this rule, then you're doing an unchecked read on the Scanner and are asking for trouble. The same is true for any of the Scanner#hasNextXXX()/Scanner#nextXXX() method pairs.
    Oh.. Well I quite fixed the code... Read the post above me

Page 1 of 2 12 LastLast

Similar Threads

  1. Simple Text File Editing in Java help
    By DMarsh12 in forum New To Java
    Replies: 1
    Last Post: 10-22-2011, 09:55 PM
  2. editing a file. Am I doing this right?
    By Mokomi in forum New To Java
    Replies: 2
    Last Post: 10-14-2011, 04:40 AM
  3. sound editing
    By nikkka in forum Advanced Java
    Replies: 3
    Last Post: 04-14-2011, 02:34 PM
  4. FileWriter editing hosts file Mac
    By danborgir in forum New To Java
    Replies: 2
    Last Post: 01-12-2011, 11:23 AM
  5. Photo Editing
    By stekun in forum Advanced Java
    Replies: 3
    Last Post: 02-23-2010, 05:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •