-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperationsExample.java
More file actions
39 lines (30 loc) · 1.1 KB
/
Copy pathFileOperationsExample.java
File metadata and controls
39 lines (30 loc) · 1.1 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
/**
* Day 22 - File Handling: File Operations
*/
import java.io.*;
public class FileOperationsExample {
public static void main(String[] args) {
System.out.println("=== File Operations ===\n");
demonstrateFileOperations();
}
static void demonstrateFileOperations() {
String fileName = "test.txt";
// Create and write
try (FileWriter writer = new FileWriter(fileName)) {
writer.write("Sample content");
System.out.println("✓ File created: " + fileName);
} catch (IOException e) {
System.out.println("Error creating file: " + e.getMessage());
}
// Check file properties
File file = new File(fileName);
System.out.println("✓ File exists: " + file.exists());
System.out.println("✓ Is file: " + file.isFile());
System.out.println("✓ File size: " + file.length() + " bytes");
System.out.println("✓ Absolute path: " + file.getAbsolutePath());
// Delete
if (file.delete()) {
System.out.println("✓ File deleted");
}
}
}