1 package com.bull.eclipse.jonas.utils; 2 3 7 8 import java.io.BufferedInputStream ; 9 import java.io.BufferedOutputStream ; 10 import java.io.BufferedReader ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.FileWriter ; 15 import java.io.IOException ; 16 import java.io.InputStreamReader ; 17 18 import com.bull.eclipse.jonas.JonasLauncherPlugin; 19 20 21 24 25 public class FileUtil { 26 27 public static String readTextFile(File f) throws IOException { 28 29 StringBuffer buf = new StringBuffer (); 30 31 BufferedReader in = new BufferedReader (new InputStreamReader (new FileInputStream (f))); 32 String inputLine; 33 while ((inputLine = in.readLine()) != null) { 34 buf.append(inputLine); 35 buf.append('\n'); 36 } 37 38 in.close(); 39 return buf.toString(); 40 41 } 42 43 public static void toTextFile(File f, String content) throws IOException { 44 FileWriter out = new FileWriter (f); 45 out.write(content); 46 out.close(); 47 } 48 49 50 public static void copy(String inputFilename, String outputFilename) throws IOException { 51 FileUtil.copy(new File (inputFilename), new File (outputFilename)); 52 } 53 54 57 public static void copy(File input, File output) throws IOException { 58 if(input.isDirectory() && output.isDirectory()) { 59 FileUtil.copyDir(input, output); 60 } else { 61 FileUtil.copyFile(input, output); 62 } 63 } 64 65 68 public static void copyFile(File inputFile, File outputFile) throws IOException { 69 JonasLauncherPlugin.log(inputFile.toString()); 70 JonasLauncherPlugin.log(outputFile.toString()); 71 BufferedInputStream fr = new BufferedInputStream (new FileInputStream (inputFile)); 72 BufferedOutputStream fw = new BufferedOutputStream (new FileOutputStream (outputFile)); 73 byte[] buf = new byte[8192]; 74 int n; 75 while((n = fr.read(buf)) >= 0) 76 fw.write(buf,0,n); 77 fr.close(); 78 fw.close(); 79 } 80 81 84 public static void copyDir(File inputDir, File outputDir) throws IOException { 85 File [] files = inputDir.listFiles(); 86 for(int i=0; i<files.length; i++) { 87 File destFile = new File (outputDir.getAbsolutePath() + File.separator + files[i].getName()); 88 if(!destFile.exists()) { 89 if(files[i].isDirectory()) { 90 destFile.mkdir(); 91 } 92 } 93 FileUtil.copy(files[i], destFile); 94 } 95 } 96 97 98 101 102 public static boolean dirContainsFiles(File dir, String extension, boolean recursive) { 103 File [] files = dir.listFiles(); 104 for(int i=0; i<files.length; i++) { 105 if(files[i].isFile() && files[i].getName().endsWith(extension)) 106 return true; 107 if(recursive && files[i].isDirectory()) 108 return FileUtil.dirContainsFiles(files[i], extension, recursive); 109 } 110 111 return false; 112 } 113 114 117 public static String readPropertyInXMLFile(File file, String property) throws IOException { 118 String content = FileUtil.readTextFile(file); 119 int startTagIdx = content.indexOf("<" + property + ">"); 120 int endTagIdx = content.indexOf("</" + property + ">"); 121 if (startTagIdx == -1) 122 throw new IOException ("Property " + property + " not found in file " + file); 123 124 return content.substring(startTagIdx + property.length() + 2, endTagIdx); 125 } 126 127 128 132 public static void removeDir(File dir) throws IOException { 133 File [] files = dir.listFiles(); 134 for (int i = 0; i < files.length; i++) { 135 if(files[i].isDirectory()) { 136 FileUtil.removeDir(files[i]); 137 } else { 138 files[i].delete(); 139 } 140 } 141 dir.delete(); 142 } 143 144 } 145 | Popular Tags |