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)
Asked
Active
Viewed 255 times
-1
-
3Have you tried anything? – ItamarG3 Nov 30 '16 at 17:14
-
1there 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 – RAZ_Muh_Taz Nov 30 '16 at 17:17
-
Please make a minimal working example : https://stackoverflow.com/help/mcve – LdiCO Nov 30 '16 at 17:21
1 Answers
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();
}

ItamarG3
- 4,092
- 6
- 31
- 44
-
@SaadAL-awad then you should accept the answer. (V under answer score) – ItamarG3 Nov 30 '16 at 19:13