-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveFileManager.java
More file actions
71 lines (63 loc) · 2.32 KB
/
saveFileManager.java
File metadata and controls
71 lines (63 loc) · 2.32 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class saveFileManager {
String fileName = "data/save.dat";
String header = "[SaveSystem] ";
public saveFileManager()
{
}
public void saveFile(int health, int strength, int heals, String name, int xP, int lvls, int maxHealth)
{
try(PrintWriter writer = new PrintWriter(new FileWriter(fileName))) {
writer.println(health+"\n"+strength+"\n"+heals+"\n"+name+"\n"+xP+"\n"+lvls+"\n"+maxHealth);
} catch (IOException e) {
System.err.println(header+"Failed to write to file!");
System.err.println(header+"An error occured: "+e.getMessage());
}
System.out.println(header+"wrote to file successfully!");
}
public String getRawSave() throws Exception
{
String data="";
Scanner scan = new Scanner(new File(fileName));
System.out.println(header+"File Exists! Getting Raw Save Data...");
for(int x=0; x<7; x++)
{
data = data+scan.nextLine()+"\n";
}
System.out.println(header+"Data Retrieved, sending to MainModule");
scan.close();
return data;
}
public void saveItemsFile(List<String> items, List<Integer> amounts )
{
try(PrintWriter writer = new PrintWriter(new FileWriter("data/items.dat"))) {
for(String item : items)
{
writer.println(item);
}
for(Integer amount : amounts)
{
writer.println(amount);
}
} catch (IOException e) {
System.err.println(header+"Failed to write to file!");
System.err.println(header+"An error occured: "+e.getMessage());
}
System.out.println(header+"wrote to file successfully!");
}
public String loadItemsFromSave() throws FileNotFoundException
{
String data="";
Scanner scan = new Scanner(new File("data/items.dat"));
System.out.println(header+"File Exists! Getting Raw Save Data...");
while(scan.hasNextLine())
{
data = scan.nextLine()+"\n"+data;
}
System.out.println(header+"Data Retrieved, sending to MainModule");
scan.close();
return data;
}
}