-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathFileUtils.java
More file actions
222 lines (196 loc) · 7.34 KB
/
FileUtils.java
File metadata and controls
222 lines (196 loc) · 7.34 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package nodebox.util;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.io.*;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
public class FileUtils {
/**
* Returns the file name without its path and extension.
* <p/>
* If the file has no extension, the file name is returned as is.
*
* @param f the file
* @return the file name without extension
*/
public static String stripExtension(File f) {
return stripExtension(f.getName());
}
/**
* Returns the file name without its path and extension.
* <p/>
* If the file has no extension, the file name is returned as is.
*
* @param fileName the file name
* @return the file name without extension
*/
public static String stripExtension(String fileName) {
int i = fileName.lastIndexOf('.');
if (i == -1) return fileName;
return fileName.substring(0, i);
}
/**
* Gets the extension of a file in lowercase.
*
* @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 in lowercase.
*
* @param fileName the file name
* @return the extension of the file.
*/
public static String getExtension(String fileName) {
int i = fileName.lastIndexOf('.');
if (i == -1) return "";
return fileName.substring(i + 1).toLowerCase(Locale.US);
}
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);
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 desc;
public FileExtensionFilter(String extensions, String desc) {
this.extensions = parseExtensions(extensions);
this.desc = desc;
}
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 desc;
}
}
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());
}
/**
* Returns the path of one File relative to another.
* <p/>
* From http://stackoverflow.com/questions/204784
*
* @param target the target directory
* @param base the base directory
* @return target's path relative to the base directory
*/
public static String getRelativePath(File target, File base) {
String[] baseComponents;
String[] targetComponents;
try {
baseComponents = base.getCanonicalPath().split(Pattern.quote(File.separator));
targetComponents = target.getCanonicalPath().split(Pattern.quote(File.separator));
} catch (IOException e) {
return target.getAbsolutePath();
}
// skip common components
int index = 0;
for (; index < targetComponents.length && index < baseComponents.length; ++index) {
if (!targetComponents[index].equals(baseComponents[index]))
break;
}
StringBuilder result = new StringBuilder();
if (index != baseComponents.length) {
// backtrack to base directory
for (int i = index; i < baseComponents.length; ++i)
result.append("..").append(File.separator);
}
for (; index < targetComponents.length; ++index)
result.append(targetComponents[index]).append(File.separator);
if (!target.getPath().endsWith("/") && !target.getPath().endsWith("\\")) {
// remove final path separator
result.delete(result.length() - "/".length(), result.length());
}
return result.toString();
}
}