-1

I have code which takes user input, so I want to save that user input into a text file. I was searching for information, but I couldn't really find anything, maybe I just don't know how to properly search. I hope that you will give me some advise on how to find a solution for my problem, to save user input into a text file.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Maksimsx
  • 9
  • 1

1 Answers1

0

It's easy:

String inputFromUser = "Anything from User";
PrintWriter out = new PrintWriter("pathToSaveAndfilename.txt");
out.println(inputFromUser);
//When your file work is completed then don't forgot to close the file
out.close();
Adarsh Raj
  • 325
  • 4
  • 17
  • As I'm using OOP way, in Main class to make my code working I'm putting for example writeName() method, to ask for user input. But how can I get only the data which user inputed? So the idea is, how can I get data x from a method in main class. – Maksimsx Apr 27 '21 at 08:46
  • File file1 = new File("out.txt"); FileWriter fw = new FileWriter(file1); PrintWriter pw = new PrintWriter(fw); pw.write(personInfo.writeName();); – Maksimsx Apr 27 '21 at 08:49
  • @Maksimsx For getting user input from file you can use `FileReader`. Here is `FileReader` [Docs](https://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html). – Adarsh Raj Apr 27 '21 at 08:56