1 | package tuffy.util; |
2 | import java.io.*; |
3 | import java.util.*; |
4 | import java.util.zip.GZIPInputStream; |
5 | import java.util.zip.GZIPOutputStream; |
6 | |
7 | /** |
8 | * Container of file related utilities. |
9 | */ |
10 | public class FileMan { |
11 | private static long fguid = 0; |
12 | |
13 | public synchronized static String getUniqueFileName(){ |
14 | long tid = Thread.currentThread().getId(); |
15 | return String.format("tmp.%5d.%5d", tid, fguid++); |
16 | } |
17 | |
18 | public synchronized static String getUniqueFileNameAbsolute(){ |
19 | long tid = Thread.currentThread().getId(); |
20 | return Config.getLoadingDir() + File.pathSeparator + String.format("tmp.%5d.%5d", tid, fguid++); |
21 | } |
22 | |
23 | public static long getFileSize(String filename) { |
24 | |
25 | File file = new File(filename); |
26 | |
27 | if (!file.exists() || !file.isFile()) { |
28 | UIMan.println("File doesn't exist"); |
29 | return -1; |
30 | } |
31 | |
32 | return file.length(); |
33 | } |
34 | |
35 | public static BufferedReader getBufferedReaderMaybeGZ(String f){ |
36 | try{ |
37 | InputStream is; |
38 | FileInputStream fis = new FileInputStream(f); |
39 | if(f.toLowerCase().endsWith(".gz")){ |
40 | is = new GZIPInputStream(fis); |
41 | }else{ |
42 | is = fis; |
43 | } |
44 | InputStreamReader reader = new InputStreamReader(is, "UTF8"); |
45 | BufferedReader lreader = new BufferedReader(reader); |
46 | return lreader; |
47 | }catch(Exception e){ |
48 | ExceptionMan.handle(e); |
49 | } |
50 | return null; |
51 | } |
52 | |
53 | |
54 | public static BufferedWriter getBufferedWriterMaybeGZ(String f){ |
55 | try{ |
56 | OutputStream os; |
57 | FileOutputStream fis = new FileOutputStream(f); |
58 | if(f.toLowerCase().endsWith(".gz")){ |
59 | os = new GZIPOutputStream(fis); |
60 | }else{ |
61 | os = fis; |
62 | } |
63 | OutputStreamWriter reader = new OutputStreamWriter(os, "UTF8"); |
64 | BufferedWriter writer = new BufferedWriter(reader); |
65 | return writer; |
66 | }catch(Exception e){ |
67 | ExceptionMan.handle(e); |
68 | } |
69 | return null; |
70 | } |
71 | |
72 | |
73 | public static String getParentDir(String fname){ |
74 | File f = new File(fname); |
75 | String p = f.getParent(); |
76 | if(p == null) return "."; |
77 | return p; |
78 | } |
79 | |
80 | /** |
81 | * Writes a string to a file, using UTF-8 encoding. |
82 | */ |
83 | public static void writeToFile(String filename, String content) { |
84 | |
85 | BufferedWriter bufferedWriter = null; |
86 | try { |
87 | bufferedWriter = new BufferedWriter(new OutputStreamWriter |
88 | (new FileOutputStream(filename),"UTF8")); |
89 | bufferedWriter.write(content); |
90 | bufferedWriter.flush(); |
91 | bufferedWriter.close(); |
92 | } catch (Exception e) { |
93 | ExceptionMan.handle(e); |
94 | } |
95 | } |
96 | |
97 | /** |
98 | * Reads lines from a text file. |
99 | */ |
100 | public static ArrayList<String> getLines(String filename){ |
101 | try { |
102 | FileReader reader = new FileReader(filename); |
103 | BufferedReader lreader = new BufferedReader(reader); |
104 | String line = lreader.readLine(); |
105 | ArrayList<String> lines = new ArrayList<String>(); |
106 | while(line != null){ |
107 | lines.add(line); |
108 | line = lreader.readLine(); |
109 | } |
110 | lreader.close(); |
111 | return lines; |
112 | }catch(IOException e) { |
113 | ExceptionMan.handle(e); |
114 | } |
115 | return null; |
116 | } |
117 | |
118 | /** |
119 | * Reads content from a text file. |
120 | */ |
121 | public static String getTextContent(String filename){ |
122 | return StringMan.join("\n", getLines(filename)); |
123 | } |
124 | |
125 | /** |
126 | * Removes a directory, even if it's NOT empty! |
127 | * @return true on success |
128 | */ |
129 | static public boolean removeDirectory(File path) { |
130 | if( path.exists() ) { |
131 | File[] files = path.listFiles(); |
132 | for(int i=0; i<files.length; i++) { |
133 | if(files[i].isDirectory()) { |
134 | removeDirectory(files[i]); |
135 | } |
136 | else { |
137 | files[i].delete(); |
138 | } |
139 | } |
140 | } |
141 | return(path.delete()); |
142 | } |
143 | |
144 | /** |
145 | * Creates an empty directory at the given path. |
146 | * If there already exists such a directory, it will |
147 | * be cleaned up. |
148 | */ |
149 | public static void createEmptyDirectory(File path){ |
150 | removeDirectory(path); |
151 | boolean ok = path.mkdir(); |
152 | if(!ok){ |
153 | ExceptionMan.die("!!! failed to create dir " + path); |
154 | } |
155 | } |
156 | |
157 | /** |
158 | * Creates the directory if it doesn't exist yet. |
159 | */ |
160 | public static void ensureExistence(String dir){ |
161 | File path = new File(dir); |
162 | if(!path.exists()){ |
163 | path.mkdirs(); |
164 | } |
165 | } |
166 | |
167 | public static String getGZIPVariant(String f){ |
168 | if(f.endsWith(".gz")){ |
169 | return f; |
170 | }else{ |
171 | String g = f + ".gz"; |
172 | if(exists(f) && exists(g)){ |
173 | File ff = new File(f); |
174 | File fg = new File(g); |
175 | String picked = null; |
176 | if(ff.lastModified() > fg.lastModified()){ |
177 | picked = f; |
178 | }else{ |
179 | picked = g; |
180 | } |
181 | UIMan.warn("Both regular and gzip'ed versions of this file exist; will use the newer one: " + picked); |
182 | return picked; |
183 | }else if(exists(g)){ |
184 | return g; |
185 | }else{ |
186 | return f; |
187 | } |
188 | } |
189 | } |
190 | |
191 | public static boolean exists(String f){ |
192 | File path = new File(f); |
193 | return path.exists(); |
194 | } |
195 | |
196 | /** |
197 | * Removes a file. |
198 | */ |
199 | public static boolean removeFile(String file){ |
200 | File f = new File(file); |
201 | return f.delete(); |
202 | } |
203 | |
204 | } |