-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathFileUtils.java
More file actions
184 lines (164 loc) · 6.27 KB
/
FileUtils.java
File metadata and controls
184 lines (164 loc) · 6.27 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 nodebox.client;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.*;
import java.util.Locale;
import java.util.StringTokenizer;
public class FileUtils {
/**
* Gets the extension of a file.
*
* @param f the file
* @return the extension of the file.
*/
public static String getExtension(File f) {
return getExtension(f.getName());
}
/**
* Gets the extension of a file.
*
* @param fileName the file name
* @return the extension of the file.
*/
public static String getExtension(String fileName) {
String ext = null;
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
ext = fileName.substring(i + 1).toLowerCase(Locale.US);
}
return ext;
}
public static File showOpenDialog(Frame owner, String pathName, String extensions, String description) {
return showFileDialog(owner, pathName, extensions, description, FileDialog.LOAD);
}
public static File showSaveDialog(Frame owner, String pathName, String extensions, String description) {
return showFileDialog(owner, pathName, extensions, description, FileDialog.SAVE);
}
private static File showFileDialog(Frame owner, String pathName, String extensions, String description, int fileDialogType) {
FileDialog fileDialog = new FileDialog(owner, pathName, fileDialogType);
if (pathName == null || pathName.trim().length() == 0) {
NodeBoxDocument document = NodeBoxDocument.getCurrentDocument();
if (document != null) {
File documentFile = document.getDocumentFile();
if (documentFile != null) {
fileDialog.setDirectory(documentFile.getParentFile().getPath());
}
}
} else {
File f = new File(pathName);
if (f.isDirectory()) {
fileDialog.setDirectory(pathName);
} else {
if (f.getParentFile() != null) {
fileDialog.setDirectory(f.getParentFile().getPath());
fileDialog.setFile(f.getName());
} else {
NodeBoxDocument document = NodeBoxDocument.getCurrentDocument();
if (document != null) {
File documentFile = document.getDocumentFile();
if (documentFile != null) {
fileDialog.setDirectory(documentFile.getParentFile().getPath());
}
}
fileDialog.setFile(pathName);
}
}
}
fileDialog.setFilenameFilter(new FileExtensionFilter(extensions, description));
fileDialog.setVisible(true);
String chosenFile = fileDialog.getFile();
String dir = fileDialog.getDirectory();
if (chosenFile != null) {
return new File(dir + chosenFile);
} else {
return null;
}
}
public static String[] parseExtensions(String extensions) {
StringTokenizer st = new StringTokenizer(extensions, ",");
String[] ext = new String[st.countTokens()];
int i = 0;
while (st.hasMoreTokens()) {
ext[i++] = st.nextToken();
}
return ext;
}
public static class FileExtensionFilter extends FileFilter implements FilenameFilter {
String[] extensions;
String description;
public FileExtensionFilter(String extensions, String description) {
this.extensions = parseExtensions(extensions);
this.description = description;
}
public boolean accept(File f) {
return f.isDirectory() || accept(null, f.getName());
}
public boolean accept(File f, String s) {
String extension = FileUtils.getExtension(s);
if (extension != null) {
for (String extension1 : extensions) {
if (extension1.equals("*") || extension1.equalsIgnoreCase(extension)) {
return true;
}
}
}
return false;
}
public String getDescription() {
return description;
}
}
public static String readFile(File file) {
StringBuffer contents = new StringBuffer();
try {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = br.readLine()) != null) {
contents.append(line);
contents.append("\n");
}
in.close();
} catch (IOException e) {
throw new RuntimeException("Could not read file " + file, e);
}
return contents.toString();
}
public static void writeFile(File file, String s) {
try {
Writer out = new BufferedWriter(new FileWriter(file));
out.write(s);
out.close();
} catch (IOException e) {
throw new RuntimeException("Could not write file " + file, e);
}
}
public static File createTemporaryDirectory(String prefix) {
File tempDir = null;
try {
tempDir = File.createTempFile(prefix, "");
} catch (IOException e) {
throw new RuntimeException("Could not create temporary file " + prefix);
}
boolean success = tempDir.delete();
if (!success) throw new RuntimeException("Could not delete temporary file " + tempDir);
success = tempDir.mkdir();
if (!success) throw new RuntimeException("Could not create temporary directory " + tempDir);
return tempDir;
}
public static boolean deleteDirectory(File directory) {
if (directory.exists()) {
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
//noinspection ResultOfMethodCallIgnored
file.delete();
}
}
}
return (directory.delete());
}
}