001 /* 002 * #%L 003 * License Maven Plugin 004 * 005 * $Id: FileUtil.java 14409 2011-08-10 15:30:41Z tchemit $ 006 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/FileUtil.java $ 007 * %% 008 * Copyright (C) 2010 - 2011 Codehaus 009 * %% 010 * This program is free software: you can redistribute it and/or modify 011 * it under the terms of the GNU Lesser General Public License as 012 * published by the Free Software Foundation, either version 3 of the 013 * License, or (at your option) any later version. 014 * 015 * This program is distributed in the hope that it will be useful, 016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 018 * GNU General Lesser Public License for more details. 019 * 020 * You should have received a copy of the GNU General Lesser Public 021 * License along with this program. If not, see 022 * <http://www.gnu.org/licenses/lgpl-3.0.html>. 023 * #L% 024 */ 025 package org.codehaus.mojo.license; 026 027 /* 028 * Codehaus License Maven Plugin 029 * 030 * This program is free software: you can redistribute it and/or modify 031 * it under the terms of the GNU Lesser General Public License as published by 032 * the Free Software Foundation, either version 3 of the License, or 033 * (at your option) any later version. 034 * 035 * This program is distributed in the hope that it will be useful, 036 * but WITHOUT ANY WARRANTY; without even the implied warranty of 037 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 038 * GNU Lesser General Public License for more details. 039 * 040 * You should have received a copy of the GNU Lesser General Public License 041 * along with this program. If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>. 042 */ 043 044 import org.codehaus.plexus.util.FileUtils; 045 import org.codehaus.plexus.util.IOUtil; 046 047 import java.io.*; 048 049 /** 050 * Some basic file io utilities 051 * 052 * @author pgier 053 * @author tchemit <chemit@codelutin.com> 054 * @since 1.0 055 */ 056 public class FileUtil 057 { 058 059 public static void tryClose( InputStream is ) 060 { 061 if ( is == null ) 062 { 063 return; 064 } 065 try 066 { 067 is.close(); 068 } 069 catch ( IOException e ) 070 { 071 // do nothing 072 } 073 } 074 075 public static void tryClose( OutputStream os ) 076 { 077 if ( os == null ) 078 { 079 return; 080 } 081 try 082 { 083 os.close(); 084 } 085 catch ( IOException e ) 086 { 087 // do nothing 088 } 089 } 090 091 /** 092 * Creates the directory (and his parents) if necessary. 093 * 094 * @param dir the directory to create if not exisiting 095 * @return {@code true} if directory was created, {@code false} if was no 096 * need to create it 097 * @throws IOException if could not create directory 098 */ 099 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 }