Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

FileUtilities.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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 
00066   public static String getPathFromCompleteFileName(String completeName)   {
00067     int index = completeName.lastIndexOf(File.separatorChar);
00068     if (index != -1)  {
00069       return completeName.substring(0, index+1);
00070     } else {
00071       return emptyString;
00072     }
00073   }
00074 
00081   public static String getNameFromCompleteFileName(String completeName)   {
00082     int index = completeName.lastIndexOf(File.separatorChar);
00083     if (index == -1)  {
00084       return completeName;
00085     } else {
00086       if (index != completeName.length()) {
00087         return completeName.substring(index+1, completeName.length());
00088       }
00089     }
00090     return emptyString;
00091   }
00092 
00096   public static void moveFile(String oldFileName, String newFileName)  {
00097     File newFile = new File(newFileName);
00098                  
00099     if (newFile.exists()) {
00100         newFile.delete();
00101     }
00102     File oldFile = new File(oldFileName);
00103     oldFile.renameTo(newFile);
00104     if (oldFile.exists()) {
00105       oldFile.delete();
00106     }
00107   }
00108 
00109   public static String concertToUnixFomat(String filePath)  {
00110     return filePath.replace('\\', '/');
00111   }
00112 
00113         public static byte[] readFile(String completeFileName) throws FileNotFoundException, IOException {
00114                 FileInputStream fileStream = new FileInputStream(completeFileName);
00115                 try {
00116                         //read channel XML
00117                         byte[] buffer = new byte[1024];
00118                         ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00119                         int bytes_read;
00120                         try {
00121                                 while(( bytes_read = fileStream.read(buffer)) != -1)  // Copy bytes to zipfile
00122                                         stream.write(buffer, 0, bytes_read);
00123                         } finally {
00124                                 stream.flush();
00125                                 stream.close();
00126                         }
00127                         return stream.toByteArray();
00128                 } finally {
00129                         fileStream.close();
00130                 }
00131         }
00132 
00133         public static byte[] readFile(File file) throws FileNotFoundException, IOException {
00134                 FileInputStream fileStream = new FileInputStream(file);
00135                 try {
00136                         //read channel XML
00137                         byte[] buffer = new byte[1024];
00138                         ByteArrayOutputStream stream = new ByteArrayOutputStream(buffer.length);
00139                         int bytes_read;
00140                         try {
00141                                 while(( bytes_read = fileStream.read(buffer)) != -1)  // Copy bytes to zipfile
00142                                         stream.write(buffer, 0, bytes_read);
00143                         } finally {
00144                                 stream.flush();
00145                                 stream.close();
00146                         }
00147                         return stream.toByteArray();
00148                 } finally {
00149                         fileStream.close();
00150                 }
00151         }
00152 
00158   public static final void removeAllFile(File file, String ext, boolean recurcif) {
00159     if ((recurcif) &&(file.isDirectory())) {
00160       File[] fileList = file.listFiles();
00161       for (int i=0; i<fileList.length; i++) {
00162         removeAllFile(fileList[i], ext, recurcif);
00163       }
00164     }
00165     if (ext != null)  {
00166       if (file.getName().indexOf(ext) != -1)  {
00167         file.delete();
00168       }
00169     } else  {
00170       file.delete();
00171     }
00172   }
00173 
00174 /*  public static final void moveFile(String sourcePath, String destPath)  throws FileNotFoundException, IOException {
00175     InputStream sourceFile = new BufferedInputStream(new FileInputStream(sourcePath));
00176     try {
00177       OutputStream destFile = new  BufferedOutputStream(new FileOutputStream(destPath));
00178       try {
00179         byte[] buffer = new byte[1024];
00180         int bytes_read;
00181         while(( bytes_read = sourceFile.read(buffer)) != -1)  // Copy bytes to zipfile
00182           destFile.write(buffer, 0, bytes_read);
00183       } finally {
00184         destFile.flush();
00185         destFile.close();
00186       }
00187     } finally {
00188       sourceFile.close();
00189     }
00190     File toRemove = new File(sourcePath);
00191   }*/
00192 
00193   public static final void copyFile(String sourcePath, String destPath)  throws FileNotFoundException, IOException {
00194     // create destination directory if not exist
00195     int index = destPath.lastIndexOf(File.separatorChar);
00196     if (index != -1)  {
00197       String dirPath = destPath.substring(0, index);
00198       if (dirPath.length() !=0) {
00199         File dirfile = new File(dirPath);
00200         if (!dirfile.exists()) {
00201           dirfile.mkdirs();
00202         }
00203       }
00204     }
00205 
00206     InputStream sourceFile = new BufferedInputStream(new FileInputStream(sourcePath));
00207     try {
00208       OutputStream destFile = new  BufferedOutputStream(new FileOutputStream(destPath));
00209       try {
00210         byte[] buffer = new byte[1024];
00211         int bytes_read;
00212         while(( bytes_read = sourceFile.read(buffer)) != -1)  // Copy bytes to zipfile
00213           destFile.write(buffer, 0, bytes_read);
00214       } finally {
00215         destFile.flush();
00216         destFile.close();
00217       }
00218     } finally {
00219       sourceFile.close();
00220     }
00221   }
00222 
00223 }

Generated on Wed Dec 14 21:05:33 2005 for OpenMobileIS by  doxygen 1.4.4