Results 1 to 5 of 5
  1. #1
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Adding String line to an existent .txt file

    As title of thread i'm trying to add new value (string type) in an existent file.

    say that we have a .txt file which contain "mario"
    i ask to the user a new name, and he write for expample "tony", now i want append the word "tony" in the existent file in this way:
    mario
    tony


    i wrote this code:
    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class SavingUser {
    	public void salvaUtente(String new_name){
    		
    		try {
    		    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("path to file... /usersList.txt", true)));
    		    out.println(new_name);
    		    out.close();
    		} catch (IOException e) {
    		}
    	}
    }
    BUT the problem is that the first name inserted from an user is being saved like this:
    mariotony

    and if i run again the program and insert ad example "carl"
    the program save the name like this
    mariotony
    carl

    and for a third one "fred" he save like this
    mariotony
    carl
    fred

    in short, only the first is saved next to the value existing. why? maybe because the program does not check if there is a string in the file?
    but I do not think. because otherwise it would happen with any name that is not entered in an odd position.
    how do I fix?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Adding String line to an existent .txt file

    My guess is that when you created the original file with mario, you did not include a linefeed at the end.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Adding String line to an existent .txt file

    i'm a newbie can be more clear?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Adding String line to an existent .txt file

    Quote Originally Posted by maki View Post
    i'm a newbie can be more clear?
    When you typed the word in a text file (e.g. mario) did you hit the enter key before saving the file or just leave the input cursor at the end of the word? It makes a difference.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    maki is offline Member
    Join Date
    Aug 2013
    Posts
    42
    Rep Power
    0

    Default Re: Adding String line to an existent .txt file

    ok now it's going.
    this code is the class that username in the file.
    in the main class i have:
    choose 1 if you are a registered member
    or
    choose 2 if you are a new user

    choosing 2 the program will ask a username an put it on the list, and choosing 1 ask for a username and check if it is in the list.
    BUT how i can make after have choosed 2 and give to the system the name for storage the program restart from the begin, or even better from a point that i want?
    Last edited by maki; 02-07-2015 at 03:57 PM.

Similar Threads

  1. Replies: 2
    Last Post: 03-08-2014, 08:48 AM
  2. HWPF Doc File - Handling existing string line break?
    By atoy3731 in forum Advanced Java
    Replies: 0
    Last Post: 11-14-2013, 08:00 PM
  3. Java- Writing a file and reading a file line by line
    By Nazneen Ali in forum New To Java
    Replies: 7
    Last Post: 07-20-2011, 07:56 AM
  4. Adding line numbers to code in output file
    By misterwebb in forum New To Java
    Replies: 2
    Last Post: 02-14-2011, 12:37 AM
  5. Replies: 2
    Last Post: 07-02-2010, 02:20 PM

Posting Permissions

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