Math2mat

/home/ythoma/docs/math2mat/svn/wp1/framework/m2mGUI/src/m2m/backend/utils/FileUtils.java

Go to the documentation of this file.
00001 
00019 package m2m.backend.utils;
00020 
00021 import java.io.BufferedInputStream;
00022 import java.io.File;
00023 import java.io.FileInputStream;
00024 import java.io.FileNotFoundException;
00025 import java.io.FileReader;
00026 import java.io.FileWriter;
00027 import java.io.IOException;
00028 import java.io.InputStream;
00029 import java.math.BigInteger;
00030 import java.security.DigestInputStream;
00031 import java.security.MessageDigest;
00032 import java.security.NoSuchAlgorithmException;
00033 
00034 public class FileUtils {
00035 
00036         public static String readFileAsString(String filePath){
00037                 byte[] buffer = new byte[(int) new File(filePath).length()];
00038             BufferedInputStream f = null;
00039             try {
00040                 f = new BufferedInputStream(new FileInputStream(filePath));
00041                 f.read(buffer);
00042             }
00043             catch (FileNotFoundException e) {
00044                 return new String("");
00045             }
00046             catch (IOException e) {
00047                 return new String("");
00048             }
00049             finally {
00050                 if (f != null) try { f.close(); } catch (IOException ignored) { }
00051             }
00052             return new String(buffer);
00053         }
00054 
00055         public static String getStringMd5(String buf) {
00056                 try {
00057                         MessageDigest messageDigest = MessageDigest.getInstance("MD5");
00058                     messageDigest.reset();
00059                     messageDigest.update(buf.getBytes());
00060                     final byte[] digest = messageDigest.digest();
00061         
00062                         BigInteger bigInt = new BigInteger(1,digest);
00063                         String hashtext = bigInt.toString(16);
00064                         // Now we need to zero pad it if you actually want the full 32 chars.
00065                         while(hashtext.length() < 32 ){
00066                           hashtext = "0"+hashtext;
00067                         }
00068                         return hashtext;
00069                 }
00070                 catch (NoSuchAlgorithmException e) {
00071                         return "";
00072                 }
00073         }
00074         
00075         public static String getMd5(String fileName) {
00076                 try {
00077                         MessageDigest md = MessageDigest.getInstance("MD5");
00078                         InputStream is = new FileInputStream(fileName);
00079                         try {
00080                           is = new DigestInputStream(is, md);
00081                           byte[] b=new byte[is.available()];
00082                           is.read(b,0,is.available());
00083                           // read stream to EOF as normal...
00084                         }
00085                         finally {
00086                           is.close();
00087                         }
00088                         byte[] digest = md.digest();
00089                         BigInteger bigInt = new BigInteger(1,digest);
00090                         String hashtext = bigInt.toString(16);
00091                         // Now we need to zero pad it if you actually want the full 32 chars.
00092                         while(hashtext.length() < 32 ){
00093                           hashtext = "0"+hashtext;
00094                         }
00095                         return hashtext;
00096                 }
00097                 catch (NoSuchAlgorithmException e) {
00098                         return "";
00099                 }
00100                 catch (FileNotFoundException e) {
00101                         return "";
00102                 }
00103                 catch (IOException e) {
00104                         return "";
00105                 }
00106         }
00107         
00108         public String getFileExtension(String fileName) {
00109                 //get the file extension
00110                 String ext = (fileName.lastIndexOf(".") == -1 ? "" :
00111                 fileName.substring(fileName.lastIndexOf(".")+1, fileName.length()));
00112                 return ext;
00113         }
00114 
00121         public static boolean deleteDirContent (File file, String fileToKeep) {
00122                 File[] files = file.listFiles();
00123                 
00124                 for (int i = 0; i < files.length; i++) {
00125                         if (files[i].getName().equalsIgnoreCase(fileToKeep))
00126                                 continue;
00127                         if (files[i].isDirectory()) {
00128                                 deleteDirContent(files[i], fileToKeep);
00129                                 files[i].delete();
00130                         } else {
00131                                 if (!files[i].delete())
00132                                         return false;
00133                         }
00134                 }
00135                 
00136                 return true;
00137         }
00138 
00139         public static boolean compareDataFiles(File origFile, File regenFile) {
00140                 boolean identical = true;
00141                 
00142                 try {
00143                         FileReader frOrig = new FileReader(origFile);
00144                         FileReader frRegen = new FileReader(regenFile);
00145                         
00146                         int charRead;
00147                         while((charRead = frOrig.read()) != -1) {
00148                                 if (charRead != frRegen.read()) {
00149                                         identical = false;
00150                                         break;
00151                                 }
00152                         }
00153                         if (charRead != frRegen.read())
00154                                 identical = false;
00155                         
00156                         frOrig.close();
00157                         frRegen.close();
00158                 } catch (FileNotFoundException e) {
00159                         e.printStackTrace();
00160                 } catch (IOException e) {
00161                         e.printStackTrace();
00162                 }
00163                 
00164                 return identical;
00165         }
00166         
00167         public static boolean copyFile(String orig, String copy) {
00168                 return copyFile(orig,copy,"");
00169         }
00170 
00171         public static boolean copyFile(String orig,String copy,String ignore) {
00172                 File input = new File(orig);
00173                 File output = new File(copy);
00174                 
00175                 if (input.isDirectory()) {
00176                         output.mkdir();
00177                         File[] fileList = input.listFiles();
00178                         for (int i = 0; i < fileList.length; i++) {
00179                                 String name=fileList[i].getName();
00180                                 if (!name.equalsIgnoreCase(ignore))
00181                                         if (!copyFile(fileList[i].getAbsolutePath(), output.getAbsolutePath() +  "/" + name,ignore))
00182                                                 return false;
00183                         }
00184                         return true;
00185                 } else {
00186                         int c;
00187                         try {
00188                                 FileReader in = new FileReader(input);
00189                                 FileWriter out = new FileWriter(output);
00190                                 
00191                                 while((c = in.read()) != -1) {
00192                                         out.write((char)c);
00193                                 }
00194                                 in.close();
00195                                 out.close();
00196                         } catch (IOException e) {
00197                                 e.printStackTrace();
00198                                 return false;
00199                         }
00200                         return true;
00201                 }
00202         }
00203         
00204         /*
00205 
00206 
00207         private boolean copyFile(String orig, String copy) {
00208                 File input = new File(orig);
00209                 File output = new File(copy);
00210                 
00211                 if (!input.exists()) {
00212                         System.err.println("File not found : " + input.getAbsolutePath());
00213                         return false;
00214                 }
00215                 
00216                 int c;
00217                 try {
00218                         FileReader in = new FileReader(input);
00219                         FileWriter out = new FileWriter(output);
00220                         
00221                         while((c = in.read()) != -1) {
00222                                 out.write((char)c);
00223                         }
00224                         in.close();
00225                         out.close();
00226                 } catch (IOException e) {
00227                         e.printStackTrace();
00228                         return false;
00229                 }
00230                 return true;
00231         }
00232         
00233         public static boolean copyFile(String orig, String copy) {
00234                 File input = new File(orig);
00235                 File output = new File(copy);
00236                 
00237                 int c;
00238                 try {
00239                         FileReader in = new FileReader(input);
00240                         FileWriter out = new FileWriter(output);
00241                         
00242                         while((c = in.read()) != -1) {
00243                                 out.write((char)c);
00244                         }
00245                         in.close();
00246                         out.close();
00247                 } catch (IOException e) {
00248                         e.printStackTrace();
00249                         return false;
00250                 }
00251                 return true;
00252         }
00253         
00254         public static void copyFile(String srFile, String dtFile) {
00255                 try {
00256                         File f1 = new File(srFile);
00257                         File f2 = new File(dtFile);
00258                         InputStream in = new FileInputStream(f1);
00259 
00260                         //For Append the file.
00261                         //      OutputStream out = new FileOutputStream(f2,true);
00262 
00263                         //For Overwrite the file.
00264                         OutputStream out = new FileOutputStream(f2);
00265 
00266                         byte[] buf = new byte[1024];
00267                         int len;
00268                         while ((len = in.read(buf)) > 0) {
00269                                 out.write(buf, 0, len);
00270                         }
00271                         in.close();
00272                         out.close();
00273                         System.out.println("File copied.");
00274                 } catch (FileNotFoundException ex) {
00275                         System.out.println(ex.getMessage() + " in the specified directory.");
00276                         System.exit(0);
00277                 } catch (IOException e) {
00278                         System.out.println(e.getMessage());
00279                 }
00280         }
00281         */
00282 }
 All Classes Namespaces Files Functions Variables Enumerations