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

Thread: Storing info into a text file & adding to it

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Storing info into a text file & adding to it

    I am trying to make a program that stores a Username and Password for a user.
    But i'm trying to have them re-edit the same file. So I want to read in the file re-write it then
    add more information to it.

    My only issue is that i would like to read in the text file and re-write it to keep the same formatting.
    I have looked up different ways to rewrite it but they all end up rewriting the file in one line.
    If anyone could offer any advice or help me it would be much appreciated.

    Here is my code so far:

    public static void main(String[] args) throws FileNotFoundException, IOException {

    JFileChooser chooser;

    //Lets user choose file.
    chooser = new JFileChooser();
    int result = chooser.showOpenDialog(null);

    if (result != JFileChooser.APPROVE_OPTION)
    return;
    File info = chooser.getSelectedFile();

    //Asks user to input new email address & password.
    String email = JOptionPane.showInputDialog("Enter new e-mail address:");
    String PW = JOptionPane.showInputDialog("Enter that e-mail's password:");

    try
    {

    BufferedWriter out = new BufferedWriter(new FileWriter(info));

    out.write("------------------");
    out.newLine();
    out.write("E-mail: " + email);
    out.newLine();
    out.write("Password: " + PW);
    out.newLine();
    out.write("------------------");
    out.close();


    }catch(IOException e) {}
    System.exit(0);
    }


    BTW i'm just using a text file.
    Thanks in Advanced.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    11
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Storing info into a text file & adding to it

    Hi,

    You have to use the constructor new FileWriter(info, true) to add text to the file.

    work?

  3. The Following User Says Thank You to tiagoufrj For This Useful Post:

    BlackFlame (February 12th, 2011)

  4. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Storing info into a text file & adding to it

    Quote Originally Posted by BlackFlame View Post

    try
    {

    BufferedWriter out = new BufferedWriter(new FileWriter(info));

    out.write("------------------");
    out.newLine();
    out.write("E-mail: " + email);
    out.newLine();
    out.write("Password: " + PW);
    out.newLine();
    out.write("------------------");
    out.close();


    }catch(IOException e) {}
    System.exit(0);
    }

    I added this to it:

    FileWriter fstream = new FileWriter(info,true);
    BufferedWriter out = new BufferedWriter(fstream);

    and it works perfectly Thank you for your help.
    Seems simple enough but it eluded me

    Thanks again

Similar Threads

  1. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  2. 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
  3. Storing data from a socket to a file in real time
    By colossusdub in forum Java Networking
    Replies: 0
    Last Post: March 2nd, 2010, 08:10 AM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM