-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.java
More file actions
134 lines (121 loc) · 4.09 KB
/
Copy pathFileUtil.java
File metadata and controls
134 lines (121 loc) · 4.09 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
import java.io.*;
import java.util.*;
import java.util.logging.*;
public class FileUtil {
public static List<String> Read(String filename) {
sLogger.fine("Read(" + filename + ")");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filename));
return Read(br);
} catch(IOException ioe) {
sLogger.log(Level.WARNING, "Unable to load " + filename, ioe);
} finally {
Close(br);
}
return new ArrayList<String>();
}
public static List<String> GetFilename(String[] args) {
List<String> filenames = new ArrayList<String>(args.length);
for(String arg : args) {
if(arg.startsWith("*")) {
final String suffix = arg.substring(1);
filenames.addAll(GetFilename(new File(".").list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(suffix) && name.substring(0, name.length() - suffix.length()).indexOf(".") < 0;
}
})));
} else {
filenames.add(arg);
}
}
return filenames;
}
public static Properties LoadProperties(String filename) {
Properties properties = new Properties();
InputStream in = FileUtil.class.getClassLoader().getResourceAsStream(filename);
try {
properties.load(in);
} catch(IOException ioe) {
sLogger.log(Level.WARNING, "Unable to load " + filename, ioe);
}
sLogger.fine("loadProperties: " + properties);
return properties;
}
public static void Copy(Properties source, Map<String,Float> target) {
for(Iterator i = source.entrySet().iterator(); i.hasNext(); ) {
Map.Entry<String,String> entry = (Map.Entry<String,String>)i.next();
target.put(entry.getKey(), Float.valueOf(entry.getValue()));
}
sLogger.fine("Copy: " + target);
}
public static List<String> Read(BufferedReader br) {
List<String> lines = new ArrayList<String>();
try {
String line;
while((line = br.readLine()) != null) {
lines.add(line);
}
} catch(IOException ioe) {
sLogger.log(Level.WARNING, "Unable to read from " + br, ioe);
}
sLogger.fine("Read Loaded " + lines.size() + " line(s).");
return lines;
}
private static void Close(BufferedReader br) {
if(br != null) {
try {
br.close();
} catch(Exception e) {
//ignore
}
}
}
public static List<String> ReadResource(String filename) {
sLogger.fine("ReadResource(" + filename + ")");
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(FileUtil.class.getClassLoader().getResourceAsStream(filename)));
return Read(br);
} finally {
Close(br);
}
}
public static String Concatenate(List<String> lines) {
StringBuffer sb = new StringBuffer();
for(String line : lines) {
sb.append(line).append("\r\n");
}
return sb.toString();
}
public static boolean Write(String filename, String line) {
List<String> lines = new ArrayList<String>();
lines.add(line);
return Write(filename, lines);
}
public static boolean Write(String filename, List<String> lines) {
sLogger.fine("Write(" + filename + ")");
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(filename));
for(String line : lines) {
bw.write(line, 0, line.length());
bw.newLine();
}
} catch(IOException ioe) {
sLogger.log(Level.WARNING, "Unable to write " + filename, ioe);
return false;
} finally {
if(bw != null) {
try {
bw.close();
} catch(Exception e) {
//ignore
}
}
}
sLogger.fine("Write wrote " + lines.size() + " line(s).");
return true;
}
private static Logger sLogger = Logger.getLogger(FileUtil.class.getName());
}