-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystemExample.java
49 lines (39 loc) · 1.5 KB
/
FileSystemExample.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
48
49
package filesystemexample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileSystemExample {
public static void main(String[] args) {
File file = new File("/temp/test.log");
File directory = new File("/flash");
String[] files = directory.list();
System.out.println("Listing files in the Flash folder.\n");
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
System.out.println(String.format("\nPath for sample file is: %s", file.getAbsolutePath()));
// Write content to the file
try ( BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) {
String fileContent = "This is a sample text.";
bufferedWriter.write(fileContent);
} catch (IOException e) {
// Exception handling
}
// Read content from the file
try ( BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
String line = bufferedReader.readLine();
while (line != null) {
System.out.println(line);
line = bufferedReader.readLine();
}
} catch (FileNotFoundException e) {
// Exception handling
} catch (IOException e) {
// Exception handling
}
}
}