-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExample.java
More file actions
35 lines (30 loc) · 805 Bytes
/
FileExample.java
File metadata and controls
35 lines (30 loc) · 805 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
import java.io.*;
public class FileExample
{
public static void main(String[] args) throws IOException
{
try {
File myFile = new File("demo.txt");
// File create
if (myFile.createNewFile()) {
System.out.println("File created: " + myFile.getName());
} else {
System.out.println("File already exists.");
}
// File properties check
System.out.println("Readable: " + myFile.canRead());
System.out.println("Writable: " + myFile.canWrite());
System.out.println("Size: " + myFile.length() + " bytes");
// File delete
if (myFile.delete()) {
System.out.println("File deleted successfully.");
} else {
System.out.println("Failed to delete file.");
}
}
catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}