-
Notifications
You must be signed in to change notification settings - Fork 1
/
Basic_FileWriterV2.java
48 lines (35 loc) · 1.26 KB
/
Basic_FileWriterV2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//2019 Java Learning Repo
//Basic File Writer V2
//written by rwx777
//Problems:
//1. Code doesn't handle spaces well
//Solved changing next() to nextLine()
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Basic_FileWriterV2 {
public static void main(String[] args) throws IOException {
Scanner reader = new Scanner(System.in);
String Text_to_Write;
String Path; // Path to File to be stored
System.out.println("Please enter the Text you want to write into the File: ");
Text_to_Write = reader.nextLine();
System.out.println("Please enter the File Path where to want to store the Text: ");
Path = reader.nextLine();
if (!Text_to_Write.isEmpty() && !Path.isEmpty()) {
try (FileWriter fileWriter = new FileWriter(Path, true)) { // Set true for append mode
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.println(Text_to_Write); // new line
printWriter.close();
System.out.println("All Done Sir");
} catch (FileNotFoundException e) {
e.printStackTrace(); // Print out Errors if something goes wrong
}
} else {
System.out.println("Usage: <Text to File> <FileLocation>");
}
reader.close();
}
}