-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate a file
More file actions
38 lines (33 loc) · 1.08 KB
/
Copy pathCreate a file
File metadata and controls
38 lines (33 loc) · 1.08 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
package taoshiflex.lab9;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Lab9 {
public static void main(String[] args) {
try {
// Create directory
File directory = new File("C:\\Users\\User\\Desktop\\university");
if (!directory.exists()) {
directory.mkdir();
}
// Create file
File file = new File(directory, "student.txt");
if (!file.exists()) {
file.createNewFile();
}
// Write data to file
try (FileWriter writer = new FileWriter(file)) {
writer.write("Name: Gazi Taoshif\nID: 12345\nDepartment: CSE\n");
}
// Read data from file
try (Scanner scan = new Scanner(file)) {
while (scan.hasNextLine()) {
System.out.println(scan.nextLine());
}
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}