-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessing.java
More file actions
184 lines (156 loc) · 4.74 KB
/
Processing.java
File metadata and controls
184 lines (156 loc) · 4.74 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package speedmatch;
import java.io.*;
import javax.swing.JOptionPane;
/**
*
* @author Jochen
*
*/
public class Processing {
// Detects which operating system SpeedMatch is running on
public static String detectOS() {
String os = System.getProperty("os.name").toLowerCase();
if(os.contains("win")) {
return "Windows";
}
else if(os.contains("nix")||os.contains("nux")||os.contains("mac")) {
return "Unix";
}
else {
return "other";
}
}
// Returns Path separator
public static char getPathSep() {
char sep = '/';
if(detectOS().equals("Windows")) {
sep = '\\';
}
return sep;
}
// Returns the working directory of the program in system-specific formatting
public static String getHomeDir() {
return System.getProperty("user.home")+ getPathSep()+"SpeedMatch"+getPathSep();
}
// Get system-specific Line separator
public static String getLineSep() {
String newline = "\n";
if(detectOS().equals("Windows")) {
newline = "\r\n";
}
return newline;
}
// Serializes a Dating-Object and writes it to a .sdo file in a given
// directory
public static void saveDating(Dating d, String dir) throws MatchException {
String outdir = dir + d.getName() + ".sdo";
File outfile = new File(outdir);
try {
FileOutputStream fout = new FileOutputStream(outfile);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(d);
oout.flush();
fout.flush();
oout.close();
fout.close();
}catch(Exception e) {
e.printStackTrace();
}
}
// OVERRIDE: Same as above but using default directory
public static void saveDating(Dating d) throws MatchException {
String outdir = getHomeDir() + d.getName() + ".sdo";
File outfile = new File(outdir);
try {
FileOutputStream fout = new FileOutputStream(outfile);
ObjectOutputStream oout = new ObjectOutputStream(fout);
oout.writeObject(d);
oout.flush();
fout.flush();
oout.close();
fout.close();
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error: "+e.getMessage(),"ERROR", JOptionPane.ERROR_MESSAGE);
}
}
// Reads saved dating from disk
public static Dating readDating(String path) throws MatchException{
File infile = new File(path);
if(infile.isDirectory()) {
throw new MatchException("Error: You specified a directory where a file is required");
}
if(!infile.canRead()) {
throw new MatchException("Error: Cannot read input file");
}
try {
FileInputStream fin = new FileInputStream(infile);
ObjectInputStream oin = new ObjectInputStream(fin);
Dating d = (Dating) oin.readObject();
oin.close();
fin.close();
return d;
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error: "+e.getMessage(),"ERROR", JOptionPane.ERROR_MESSAGE);
return null;
}
}
// Same as above, but using default directory
public static Dating readDatingFromWD(String filename) throws MatchException{
String indir = getHomeDir() + filename;
File infile = new File(indir);
if(infile.isDirectory()) {
throw new MatchException("Error: You specified a directory where a file is required");
}
if(!infile.canRead()) {
throw new MatchException("Error: Cannot read input file");
}
try {
FileInputStream fin = new FileInputStream(infile);
ObjectInputStream oin = new ObjectInputStream(fin);
Dating d = (Dating) oin.readObject();
oin.close();
fin.close();
return d;
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "Error: "+e.getMessage(),"ERROR", JOptionPane.ERROR_MESSAGE);
return null;
}
}
// Returns an array containing the Names of .sdo-files in a specific directory
public static String[] getDatingsDir(String path) throws IOException {
File dir = new File(path);
if(dir.isDirectory()) {
String[] farr = dir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".sdo");
}});
return farr;
}
else {
throw new IOException("Not a directory");
}
}
// OVERRIDE: Same as above, but using default directory
public static String[] getDatingsDir() throws IOException{
return getDatingsDir(getHomeDir());
}
//Delete a file from directory
public static void deleteFile (String path, String filename) {
File f = new File(path+filename);
if(f.exists()) {
try {
f.delete();
}catch(Exception e) {
JOptionPane.showMessageDialog(null, "An unknown error occured while deleting the file", "ERROR", JOptionPane.ERROR_MESSAGE);;
}
}
else {
JOptionPane.showMessageDialog(null, "Unable to access file. Please check read/write permissions", "ERROR", JOptionPane.ERROR_MESSAGE);;
}
}
//OVERRIDE: Same as above, but using default directory
public static void deleteFile(String filename) {
deleteFile(getHomeDir(),filename);
}
// Writes a String to a file
}