-1

I am new in this so I hope some one can help me I want to know How to take string input from user's in Java and save it to a txt. file ? (and please explain how if you can)

3
  • 3
    Have you tried anything? Commented Nov 30, 2016 at 17:14
  • 1
    there are plenty of examples of this on stackoverflow, just go to the top right and enter the title of your problem in that search bar Commented Nov 30, 2016 at 17:17
  • Please make a minimal working example : stackoverflow.com/help/mcve Commented Nov 30, 2016 at 17:21

1 Answer 1

0

You can use Scanner and FileWriter:

Scanner s = new Scanner(System.in);
String text = s.nextLine();//read input from user
File f = new File("C:\\Users\\itama\\Desktop\\filename.txt");
try {
    FileWriter fw = new FileWriter(f);
    fw.write(text);
    fw.flush();
    fw.close();
} catch (IOException e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@SaadAL-awad then you should accept the answer. (V under answer score)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.