-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.java
More file actions
28 lines (25 loc) · 1.01 KB
/
FileHandler.java
File metadata and controls
28 lines (25 loc) · 1.01 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
import java.io.*;
import java.util.Scanner;
public class FileHandler {
public StudentGradeBST loadData(String filename) {
StudentGradeBST tree = new StudentGradeBST();
try (Scanner scanner = new Scanner(new File(filename))) {
while (scanner.hasNextLine()) {
String[] parts = scanner.nextLine().split(",");
String name = parts[0];
double grade = Double.parseDouble(parts[1]);
tree.insertOrUpdate(new Student(name, grade));
}
} catch (FileNotFoundException e) {
System.out.println("File not found. Starting with an empty database.");
}
return tree;
}
public void saveData(StudentGradeBST tree, String filename) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
tree.saveInOrder(writer, tree.getRoot());
} catch (IOException e) {
System.out.println("Error saving data: " + e.getMessage());
}
}
}