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