-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveGame.java
More file actions
62 lines (56 loc) · 2.08 KB
/
Copy pathSaveGame.java
File metadata and controls
62 lines (56 loc) · 2.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package modular;
import data.Game;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.HashMap;
public class SaveGame {
public static void saveFileOtherPath() {
String fileName = "game.gobang";
File file = new File(fileName);
JFileChooser jFileChooser = new JFileChooser("./");
int i = 1;
while (file.exists()) {
fileName = "game" + "(" + i + ")" + ".gobang";
file = new File(fileName);
i++;
}
jFileChooser.setSelectedFile(file);
jFileChooser.showSaveDialog(new JFrame());
file = jFileChooser.getSelectedFile();
if (!file.exists()) {
try {
if (!file.createNewFile()) throw new IOException();
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "닸도댄轎");
}
}
try {
HashMap<String, Object> game = Game.getGame();
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(game);
} catch (IOException e) {
JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), "닸도댄轎");
}
}
public static void saveFileDefaultPath() {
HashMap<String, Object> game = Game.getGame();
File file = new File("game.gobang");
try {
if (!file.exists()) {
if (!(file.createNewFile())) {
JOptionPane.showMessageDialog(new JFrame(), "뇨닸토零댄轎");
return;
}
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(game);
} catch (IOException e) {
JOptionPane.showMessageDialog(new JFrame(), "닸도댄轎");
}
}
}