-
Notifications
You must be signed in to change notification settings - Fork 1
/
WriteToFile.java
23 lines (20 loc) · 916 Bytes
/
WriteToFile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;
import java.util.Scanner;
public class WriteToFile {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.print("Provide Path(C:\\\\..) or Enter File Name with Extension(if present in the same directory): ");
String fileName = sc.nextLine();
try {
FileWriter fw = new FileWriter(fileName);
System.out.println("Type Here to write on \""+fileName+"\" :");
String line = sc.nextLine();
fw.write(line);
fw.close();
System.out.println("\nContents successfully written to \""+fileName+"\"\n");
}
catch(FileNotFoundException e) {
System.out.println("\nFile not found with the name specified!!\nPlease check and provide the name correctly!\n");
}
}
}