Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/muffin/FileMuffin.java
1461 views
1
package org.newdawn.slick.muffin;
2
3
import java.io.EOFException;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileOutputStream;
7
import java.io.IOException;
8
import java.io.ObjectInputStream;
9
import java.io.ObjectOutputStream;
10
import java.util.HashMap;
11
12
import org.newdawn.slick.util.Log;
13
14
/**
15
* An implementation of the muffin load/save mechanism based around using the
16
* local file system.
17
*
18
* @author kappaOne
19
*/
20
public class FileMuffin implements Muffin {
21
22
/**
23
* @see org.newdawn.slick.muffin.Muffin#saveFile(java.util.HashMap,
24
* java.lang.String)
25
*/
26
public void saveFile(HashMap scoreMap, String fileName) throws IOException {
27
String userHome = System.getProperty("user.home");
28
File file = new File(userHome);
29
file = new File(file, ".java");
30
if (!file.exists()) {
31
file.mkdir();
32
}
33
34
file = new File(file, fileName);
35
FileOutputStream fos = new FileOutputStream(file);
36
ObjectOutputStream oos = new ObjectOutputStream(fos);
37
38
// save hashMap
39
oos.writeObject(scoreMap);
40
41
oos.close();
42
}
43
44
/**
45
* @see org.newdawn.slick.muffin.Muffin#loadFile(java.lang.String)
46
*/
47
public HashMap loadFile(String fileName) throws IOException {
48
HashMap hashMap = new HashMap();
49
String userHome = System.getProperty("user.home");
50
51
File file = new File(userHome);
52
file = new File(file, ".java");
53
file = new File(file, fileName);
54
55
if (file.exists()) {
56
try {
57
FileInputStream fis = new FileInputStream(file);
58
ObjectInputStream ois = new ObjectInputStream(fis);
59
60
hashMap = (HashMap) ois.readObject();
61
62
ois.close();
63
64
} catch (EOFException e) {
65
// End of the file reached, do nothing
66
} catch (ClassNotFoundException e) {
67
Log.error(e);
68
throw new IOException("Failed to pull state from store - class not found");
69
}
70
}
71
72
return hashMap;
73
}
74
}
75