-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreating_File.java
More file actions
42 lines (33 loc) · 808 Bytes
/
Creating_File.java
File metadata and controls
42 lines (33 loc) · 808 Bytes
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
package File_Manipulaion;
import java.util.*;
import java.io.*;
public class Creating_File {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the file: ");
String filename = sc.nextLine();
File file = new File(filename+".txt");
System.out.println("Enter the content of the file: ");
String content = sc.nextLine();
sc.close();
if(!file.exists())
{
file.createNewFile();
PrintWriter pw = new PrintWriter(file);
pw.println(content);
pw.close();
System.out.println("Geneated the file successfully");
}
else
{
System.out.println("File of similar name already exists");
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}