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

Thread: Help with making the user input to store in file

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with making the user input to store in file

    i understand how to print into a file but don't understand how to allow the user to input text so that it stores in the text file.

    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
     
    /**
     * Demonstrates how to append text to a file using Java.
     * (Just add a "true" boolean parameter when creating the FileWriter.)
     * Created by Alvin Alexander, [url=http://devdaily.com]devdaily.com | tutorials for java, linux, mac os x, iphone, ipad, uml, quality, testing[/url].
     */
    public class reservation
    {
     
      public static void main(String[] args)
      {
        PrintWriter pw = null;
        try
        {
          // created as a separate variable to emphasize that I'm appending to this file
          boolean append = true;
          pw = new PrintWriter(new FileWriter(new File("output.txt"), append));
          // a print writer gives you many more methods to write with
          pw.println("Hello, World");
        }
        catch (IOException e)
        {
          e.printStackTrace();
          // deal with the exception
        }
        finally
        {
          pw.close();
        }
      }
     
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help with making the user input to store in file

    Is this a duplicated thread? Your other thread - http://www.javaprogrammingforums.com...html#post30136
    Please keep all the information in 1 thread rather than starting a new one. I have closed your other thread.

    This is easy to do using the link I provided you - http://www.javaprogrammingforums.com...ner-class.html

        Scanner sc = new Scanner(System.in);
        System.out.println("Add something: ");
        String newText = sc.nextLine();
     
        pw.println(newText);
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Allow user input to edit text file??
    By dannyyy in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2011, 06:53 AM
  2. [SOLVED] Help with getting user input for my factorial method
    By u-will-neva-no in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 4th, 2011, 01:24 PM
  3. user input strings to vectors
    By vudoo in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 7th, 2011, 10:30 PM
  4. Help with toString and accesing user input
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 07:58 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM