FileUtilities.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2006 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: pdelrieu@openmobileis.org
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00020  * USA
00021  *
00022  *  Author : Philippe Delrieu
00023  *  
00024  *  Modifications :
00025  *  2004 Creation P.Delrieu
00026  * 
00027  */
00028 
00029 package org.openmobileis.common.util.file;
00030 
00031 import java.io.*;
00032 
00036 public class FileUtilities {
00037   private static final String emptyString = "";
00038 
00043   public static String convertFileNameToSystem(String zipFile) {
00044     String ret =null;
00045     switch (File.separatorChar) {
00046       case '/' :
00047         ret = zipFile.replace('\\', '/');
00048         break;
00049       case '\\' :
00050         ret = zipFile.replace('/', '\\');
00051         break;
00052       default : 
00053         ret = zipFile;
00054         break;
00055     }
00056 
00057     return ret;
00058   }
00059   // Deletes all files and subdirectories under dir.
00060   // Returns true if all deletions were successful.
00061   // If a deletion fails, the method stops attempting to delete and returns false.
00062   
00069   public static boolean deleteDir(File dir) {
00070       if (dir.isDirectory()) {
00071           String[] children = dir.list();
00072           for (int i=0; i<children.length; i++) {
00073               boolean success = deleteDir(new File(dir, children[i]));
00074               if (!success) {
00075                   return false;
00076               }
00077           }
00078       }
00079       return dir.delete();
00080   }
00087   public static String getPathFromCompleteFileName(String completeName)   {
00088     int index = completeName.lastIndexOf(File.separatorChar);
00089     if (index != -1)  {
00090       return completeName.substring(0, index+1);
00091     } else {
00092       return emptyString;
00093     }
00094   }
00095 
00102   public static String getNameFromCompleteFileName(String completeName)   {
00103     int index = completeName.lastIndexOf(File.separatorChar);
00104     if (index == -1)  {
00105       return completeName;
00106     } else {
00107       if (index != completeName.length()) {
00108         return completeName.substring(index+1, completeName.length());
00109       }
00110     }
00111     return emptyString;
00112   }
00113 
00117   public static void moveFile(String oldFileName, String newFileName)  {
00118     File newFile = new File(newFileName);
00119                  
00120     if (newFile.exists()) {
00121         newFile.delete();
00122     }
00123     File oldFile = new File(oldFileName);
00124     oldFile.renameTo(newFile);
00125     if (oldFile.exists()) {
00126       oldFile.delete();
00127     }
00128   }
00129 
00130   public static String convertToUnixFomat(String filePath)  {
00131     return filePath.replace('\\', '/');
00132   }
00133 
00134         public static byte[] readFile(String completeFileName) throws FileNotFoundException, IOException {
00135                 FileInputStream fileStream = new FileInputStream(completeFileName);
00136                 try {
00137                         //read channel XML
00138                         byte[] buffer = new byte[1024];
00139                         ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00140                         int bytes_read;
00141                         try {
00142                                 while(( bytes_read = fileStream.read(buffer)) != -1)  // Copy bytes to zipfile
00143                                         stream.write(buffer, 0, bytes_read);
00144                         } finally {
00145                                 stream.flush();
00146                                 stream.close();
00147                         }
00148                         return stream.toByteArray();
00149                 } finally {
00150                         fileStream.close();
00151                 }
00152         }
00153 
00154         public static byte[] readFile(File file) throws FileNotFoundException, IOException {
00155                 FileInputStream fileStream = new FileInputStream(file);
00156                 try {
00157                         //read channel XML
00158                         byte[] buffer = new byte[1024];
00159                         ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00160                         int bytes_read;
00161                         try {
00162                                 while(( bytes_read = fileStream.read(buffer)) != -1)  // Copy bytes to zipfile
00163                                         stream.write(buffer, 0, bytes_read);
00164                         } finally {
00165                                 stream.flush();
00166                                 stream.close();
00167                         }
00168                         return stream.toByteArray();
00169                 } finally {
00170                         fileStream.close();
00171                 }
00172         }
00173 
00179   public static final void removeAllFile(File file, String ext, boolean recurcif) {
00180     if ((recurcif) &&(file.isDirectory())) {
00181       File[] fileList = file.listFiles();
00182       for (int i=0; i<fileList.length; i++) {
00183         removeAllFile(fileList[i], ext, recurcif);
00184       }
00185     }
00186     if (ext != null)  {
00187       if (file.getName().indexOf(ext) != -1)  {
00188         file.delete();
00189       }
00190     } else  {
00191       file.delete();
00192     }
00193   }
00194 
00195 /*  public static final void moveFile(String sourcePath, String destPath)  throws FileNotFoundException, IOException {
00196     InputStream sourceFile = new BufferedInputStream(new FileInputStream(sourcePath));
00197     try {
00198       OutputStream destFile = new  BufferedOutputStream(new FileOutputStream(destPath));
00199       try {
00200         byte[] buffer = new byte[1024];
00201         int bytes_read;
00202         while(( bytes_read = sourceFile.read(buffer)) != -1)  // Copy bytes to zipfile
00203           destFile.write(buffer, 0, bytes_read);
00204       } finally {
00205         destFile.flush();
00206         destFile.close();
00207       }
00208     } finally {
00209       sourceFile.close();
00210     }
00211     File toRemove = new File(sourcePath);
00212   }*/
00213 
00214   public static final void copyFile(String sourcePath, String destPath)  throws FileNotFoundException, IOException {
00215     // create destination directory if not exist
00216     int index = destPath.lastIndexOf(File.separatorChar);
00217     if (index != -1)  {
00218       String dirPath = destPath.substring(0, index);
00219       if (dirPath.length() !=0) {
00220         File dirfile = new File(dirPath);
00221         if (!dirfile.exists()) {
00222           dirfile.mkdirs();
00223         }
00224       }
00225     }
00226 
00227     InputStream sourceFile = new BufferedInputStream(new FileInputStream(sourcePath));
00228     try {
00229       OutputStream destFile = new  BufferedOutputStream(new FileOutputStream(destPath));
00230       try {
00231         byte[] buffer = new byte[1024];
00232         int bytes_read;
00233         while(( bytes_read = sourceFile.read(buffer)) != -1)  // Copy bytes to zipfile
00234           destFile.write(buffer, 0, bytes_read);
00235       } finally {
00236         destFile.flush();
00237         destFile.close();
00238       }
00239     } finally {
00240       sourceFile.close();
00241     }
00242   }
00243 
00244 }

Generated on Mon Jan 11 21:19:14 2010 for OpenMobileIS by  doxygen 1.5.4