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

Thread: trying to save data onto a file

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default trying to save data onto a file

    Hey im trying to make a program that creates a new file and saves any amount of strings into the file, and basically the problem is that it only saves the last string that the user inputted. How do i make it save all the strings that the user inputs. Here is the code i have:

     
        PrintWriter output;
        int line = 0;
        int i;
        int total;
        c.print ("Enter the number of words you're going to input: ");
            total = c.readInt ();
            c.println ();
     
            String[] word = new String [total];
     
            do
            {
                try
                {
                    output = new PrintWriter (new FileWriter ("DataIn.txt"));
     
                    c.print ("Enter word " + (line + 1) + (": "));
                    word [line] = c.readString ();
     
                    output.println (word [line]);
                    line++;
     
                    output.close ();
     
     
     
                }
                catch (IOException e)
                {
                    new Message ("Error");
                }
     
            }
            while (line < total);


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: trying to save data onto a file

    Each time you open the FileWriter, it will overwrite the previous contents. Either open the File outside the loop, or take a look at the API - in particular the constructor that accepts a boolean value to append to a file
    FileWriter (Java Platform SE 6)

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trying to save data onto a file

    I put the filewriter outside the loop and it still only reads the last string that the user will input, here is the code:

    try
    {
    output = new PrintWriter (new FileWriter ("DataIn.txt"));
    }
    catch (IOException e)
    {
    new Message ("Error");
    }

    do
    {

    c.print ("Enter word " + (line + 1) + (": "));
    word [line] = c.readString ();

    output.println (word [line]);
    line++;

    output.close ();


    }
    while (line < total);

  4. #4
    Member
    Join Date
    Nov 2011
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: trying to save data onto a file

    You need to use the append() method on the PrintWriter object, instead of the println() method you have tried.

    Spring 3
    Last edited by cafeteria84; January 22nd, 2012 at 04:03 PM.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: trying to save data onto a file

    Quote Originally Posted by DanTheSand View Post
    I put the filewriter outside the loop and it still only reads the last string that the user will input, here is the code:
    I would guess that would cause only the first line to be written...the output variable is closed on the first loop - that must be moved outside as well. And for future reference, please wrap your code in the code tags

Similar Threads

  1. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 12th, 2011, 02:21 PM
  2. HELP PLEASE! How to save and call back user data input.
    By boi_boi in forum Object Oriented Programming
    Replies: 3
    Last Post: August 12th, 2011, 04:42 AM
  3. Save data entered upon reopening
    By SHStudent21 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 12th, 2011, 10:24 PM
  4. Save form as .pdf file?
    By Taron in forum AWT / Java Swing
    Replies: 3
    Last Post: November 14th, 2010, 02:20 PM
  5. Saving .jsp page as .pdf file while generating report for struts based web application
    By ravindra_kumar_tiwari in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: August 12th, 2008, 09:32 AM