View Javadoc

1   /*
2    * #%L
3    * License Maven Plugin
4    *
5    * $Id: FileUtil.java 14409 2011-08-10 15:30:41Z tchemit $
6    * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/FileUtil.java $
7    * %%
8    * Copyright (C) 2010 - 2011 Codehaus
9    * %%
10   * This program is free software: you can redistribute it and/or modify
11   * it under the terms of the GNU Lesser General Public License as 
12   * published by the Free Software Foundation, either version 3 of the 
13   * License, or (at your option) any later version.
14   * 
15   * This program is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   * GNU General Lesser Public License for more details.
19   * 
20   * You should have received a copy of the GNU General Lesser Public 
21   * License along with this program.  If not, see
22   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23   * #L%
24   */
25  package org.codehaus.mojo.license;
26  
27  /* 
28   * Codehaus License Maven Plugin
29   *     
30   * This program is free software: you can redistribute it and/or modify
31   * it under the terms of the GNU Lesser General Public License as published by
32   * the Free Software Foundation, either version 3 of the License, or
33   * (at your option) any later version.
34   *
35   * This program is distributed in the hope that it will be useful,
36   * but WITHOUT ANY WARRANTY; without even the implied warranty of
37   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38   * GNU Lesser General Public License for more details.
39   *
40   * You should have received a copy of the GNU Lesser General Public License
41   * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
42   */
43  
44  import org.codehaus.plexus.util.FileUtils;
45  import org.codehaus.plexus.util.IOUtil;
46  
47  import java.io.*;
48  
49  /**
50   * Some basic file io utilities
51   *
52   * @author pgier
53   * @author tchemit <chemit@codelutin.com>
54   * @since 1.0
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              // do nothing
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              // do nothing
88          }
89      }
90  
91      /**
92       * Creates the directory (and his parents) if necessary.
93       *
94       * @param dir the directory to create if not exisiting
95       * @return {@code true} if directory was created, {@code false} if was no
96       *         need to create it
97       * @throws IOException if could not create directory
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      * Delete the given file.
132      *
133      * @param file the file to delete
134      * @throws IOException if could not delete the file
135      */
136     public static void deleteFile( File file )
137         throws IOException
138     {
139         if ( !file.exists() )
140         {
141             // file does not exist, can not delete it
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      * Rename the given file to a new destination.
153      *
154      * @param file        the file to rename
155      * @param destination the destination file
156      * @throws IOException if could not rename the file
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      * Copy a file to a given locationand logging.
170      *
171      * @param source represents the file to copy.
172      * @param target file name of destination file.
173      * @throws IOException if could not copy file.
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      * @param file the source file
194      * @return the backup file
195      */
196     public static File getBackupFile( File file )
197     {
198         return new File( file.getAbsolutePath() + "~" );
199     }
200 
201     /**
202      * Backups the given file using the {@link FileUtil#getBackupFile(File)} as
203      * destination file.
204      *
205      * @param f the file to backup
206      * @throws IOException if any pb while copying the file
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      * Permet de lire un fichier et de retourner sont contenu sous forme d'une
217      * chaine de carateres
218      *
219      * @param file     le fichier a lire
220      * @param encoding encoding to read file
221      * @return the content of the file
222      * @throws IOException if IO pb
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      * Sauvegarde un contenu dans un fichier.
241      *
242      * @param file     le fichier a ecrire
243      * @param content  le contenu du fichier
244      * @param encoding l'encoding d'ecriture
245      * @throws IOException if IO pb
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 }