-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_Handling.java
More file actions
56 lines (48 loc) · 1.36 KB
/
Copy pathFile_Handling.java
File metadata and controls
56 lines (48 loc) · 1.36 KB
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
49
50
51
52
53
54
55
56
package BASICS;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class File_Handling {
public static void main(String[] args) throws IOException {
//Code to create a new file
/*
File myFile = new File("Harshit.txt");
try {
myFile.createNewFile();
}
catch (IOException e){
System.out.println("Unable to create file");
e.printStackTrace();
}
*/
//Code to write to a file
/*
try {
FileWriter fileWriter = new FileWriter("Harshit.txt");
fileWriter.write("This is our first file of this java Course");
fileWriter.close();
}
catch (IOException e){
e.printStackTrace();
}
*/
//Reading a file
/*
File myfile = new File("Harshit.txt");
Scanner sc = new Scanner(myfile);
while(sc.hasNextLine()){
String line = sc.nextLine();
System.out.println(line);
}
sc.close();
*/
File myfile = new File("Harshit.txt");
if(myfile.delete()){
System.out.println("I have deleted : " + myfile.getName());
}
else{
System.out.println("Some problem occured while deleting the file");
}
}
}