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.
Code Java:
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();
}
}
}
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
Code Java:
Scanner sc = new Scanner(System.in);
System.out.println("Add something: ");
String newText = sc.nextLine();
pw.println(newText);