-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadGame.java
More file actions
58 lines (52 loc) · 2.07 KB
/
Copy pathLoadGame.java
File metadata and controls
58 lines (52 loc) · 2.07 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
package modular;
import com.sun.security.auth.UnixNumericGroupPrincipal;
import data.Game;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashMap;
public class LoadGame {
public synchronized static void loadFileOtherPath() {
JFileChooser jFileChooser = new JFileChooser("./");
jFileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else {
String name = f.getName();
return name.endsWith("gobang");
}
}
@Override
public String getDescription() {
return "需要\".gobang\" 文件";
}
});
jFileChooser.showOpenDialog(new JFrame());
File file = jFileChooser.getSelectedFile();
try {
if (file == null) throw new NullPointerException();
FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
HashMap<String, Object> game = (HashMap<String, Object>) objectInputStream.readObject();
Game.initGame(game);
} catch (NullPointerException | ClassNotFoundException ignored) {
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "读取文件失败");
}
}
public synchronized static void loadFileDefaultPath() {
try {
FileInputStream fileInputStream = new FileInputStream("game.gobang");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
HashMap<String, Object> game = (HashMap<String, Object>) objectInputStream.readObject();
Game.initGame(game);
} catch (Exception Exception) {
JOptionPane.showMessageDialog(new JFrame(), "读取文件失败");
}
}
}