-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandler.java
47 lines (43 loc) · 1.21 KB
/
FileHandler.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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileHandler {
static boolean writeNewFile(String filename) {
try {
File myObj = new File(filename);
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
} else {
System.out.println("File already exists.");
return false;
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
return true;
}
static void writeToFile(String filename, String output) {
try {
FileWriter myWriter = new FileWriter(filename);
myWriter.write(output);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
static void deleteFile(String filename) {
try {
File myObj = new File(filename);
if (myObj.delete()) {
System.out.println("Deleted the file: " + myObj.getName());
} else {
System.out.println("Failed to delete the file.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}