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 package org.codehaus.mojo.license;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 import org.codehaus.plexus.util.FileUtils;
45 import org.codehaus.plexus.util.IOUtil;
46
47 import java.io.*;
48
49
50
51
52
53
54
55
56 public class FileUtil
57 {
58
59 public static void tryClose( InputStream is )
60 {
61 if ( is == null )
62 {
63 return;
64 }
65 try
66 {
67 is.close();
68 }
69 catch ( IOException e )
70 {
71
72 }
73 }
74
75 public static void tryClose( OutputStream os )
76 {
77 if ( os == null )
78 {
79 return;
80 }
81 try
82 {
83 os.close();
84 }
85 catch ( IOException e )
86 {
87
88 }
89 }
90
91
92
93
94
95
96
97
98
99 public static boolean createDirectoryIfNecessary( File dir )
100 throws IOException
101 {
102 if ( !dir.exists() )
103 {
104 boolean b = dir.mkdirs();
105 if ( !b )
106 {
107 throw new IOException( "Could not create directory " + dir );
108 }
109 return true;
110 }
111 return false;
112 }
113
114 public static boolean createNewFile( File file )
115 throws IOException
116 {
117 createDirectoryIfNecessary( file.getParentFile() );
118 if ( !file.exists() )
119 {
120 boolean b = file.createNewFile();
121 if ( !b )
122 {
123 throw new IOException( "Could not create new file " + file );
124 }
125 return true;
126 }
127 return false;
128 }
129
130
131
132
133
134
135
136 public static void deleteFile( File file )
137 throws IOException
138 {
139 if ( !file.exists() )
140 {
141
142 return;
143 }
144 boolean b = file.delete();
145 if ( !b )
146 {
147 throw new IOException( "could not delete file " + file );
148 }
149 }
150
151
152
153
154
155
156
157
158 public static void renameFile( File file, File destination )
159 throws IOException
160 {
161 boolean b = file.renameTo( destination );
162 if ( !b )
163 {
164 throw new IOException( "could not rename " + file + " to " + destination );
165 }
166 }
167
168
169
170
171
172
173
174
175 public static void copyFile( File source, File target )
176 throws IOException
177 {
178 createDirectoryIfNecessary( target.getParentFile() );
179 FileUtils.copyFile( source, target );
180 }
181
182 public static File getFile( File base, String... paths )
183 {
184 StringBuilder buffer = new StringBuilder();
185 for ( String path : paths )
186 {
187 buffer.append( File.separator ).append( path );
188 }
189 return new File( base, buffer.substring( 1 ) );
190 }
191
192
193
194
195
196 public static File getBackupFile( File file )
197 {
198 return new File( file.getAbsolutePath() + "~" );
199 }
200
201
202
203
204
205
206
207
208 public static void backupFile( File f )
209 throws IOException
210 {
211 File dst = FileUtil.getBackupFile( f );
212 copyFile( f, dst );
213 }
214
215
216
217
218
219
220
221
222
223
224 static public String readAsString( File file, String encoding )
225 throws IOException
226 {
227 FileInputStream inf = new FileInputStream( file );
228 BufferedReader in = new BufferedReader( new InputStreamReader( inf, encoding ) );
229 try
230 {
231 return IOUtil.toString( in );
232 }
233 finally
234 {
235 in.close();
236 }
237 }
238
239
240
241
242
243
244
245
246
247 public static void writeString( File file, String content, String encoding )
248 throws IOException
249 {
250 createDirectoryIfNecessary( file.getParentFile() );
251 BufferedWriter out;
252 out = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( file ), encoding ) );
253 try
254 {
255 IOUtil.copy( content, out );
256 }
257 finally
258 {
259 out.close();
260 }
261 }
262 }