1 17 18 package org.apache.lenya.util; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.StringTokenizer ; 28 29 import org.apache.log4j.Category; 30 31 34 public final class FileUtil { 35 private static Category log = Category.getInstance(FileUtil.class); 36 37 42 public static void main(String [] args) { 43 if (args.length == 0) { 44 System.err.println("Usage: java " + new FileUtil().getClass().getName()); 45 46 return; 47 } 48 49 if (args[0].equals("--copy")) { 50 if (args.length != 3) { 51 System.err.println("Usage: --copy source destination"); 52 53 return; 54 } 55 56 try { 57 System.err.println("cp " + args[1] + " " + args[2]); 58 copy(args[1], args[2]); 59 } catch (FileNotFoundException e) { 60 System.err.println(e); 61 } catch (IOException e) { 62 System.err.println(e); 63 } 64 65 return; 66 } 67 68 if (args[0].equals("--concatPath")) { 69 File file = org.apache.lenya.util.FileUtil.file( 71 "/root/temp/jpf-1.9/java/lenya/x/xps/samples/invoices/invoices", 72 "../addresses/lenya.xml"); 73 System.out.println(file.getAbsolutePath()); 74 } else { 75 } 76 } 77 78 87 public static void copy(String source_name, String destination_name) 88 throws FileNotFoundException , IOException { 89 InputStream source = new FileInputStream (source_name); 90 File destination_file = new File (destination_name); 91 File parent = new File (destination_file.getParent()); 92 93 if (!parent.exists()) { 94 parent.mkdirs(); 95 log.debug("Directory has been created: " + parent.getAbsolutePath()); 96 } 97 98 OutputStream destination = new FileOutputStream (destination_name); 99 byte[] bytes_buffer = new byte[1024]; 100 int bytes_read; 101 102 while ((bytes_read = source.read(bytes_buffer)) >= 0) { 103 destination.write(bytes_buffer, 0, bytes_read); 104 } 105 } 106 107 116 public static void copy(File src, File dest) throws FileNotFoundException , IOException { 117 118 if (src.isFile()) { 119 copySingleFile(src, dest); 120 } else { 121 File [] contents = src.listFiles(); 122 123 if (contents == null) 124 return; 125 126 dest.mkdirs(); 127 128 for (int i = 0; i < contents.length; i++) { 129 String destPath = dest.getAbsolutePath() + File.separator + contents[i].getName(); 130 copy(contents[i], new File (destPath)); 131 } 132 } 133 } 134 135 144 protected static void copySingleFile(File src, File dest) throws FileNotFoundException , 145 IOException { 146 147 dest.getParentFile().mkdirs(); 148 dest.createNewFile(); 149 org.apache.avalon.excalibur.io.FileUtil.copyFile(src, dest); 150 } 151 152 160 public static File file(String absoluteDir, String relativeFile) { 161 File file = new File (fileName(absoluteDir, relativeFile)); 162 163 return file; 164 } 165 166 175 public static String fileName(String absoluteDir, String relativeFile) { 176 String fileName = null; 177 String newAbsoluteDir = null; 178 179 if (!(absoluteDir.charAt(absoluteDir.length() - 1) == '/')) { 180 newAbsoluteDir = absoluteDir + "/"; 181 } else { 182 newAbsoluteDir = absoluteDir; 183 } 184 185 if (relativeFile.indexOf("../") == 0) { 186 StringTokenizer token = new StringTokenizer (newAbsoluteDir, "/"); 187 newAbsoluteDir = "/"; 188 189 int numberOfTokens = token.countTokens(); 190 191 for (int i = 0; i < (numberOfTokens - 1); i++) { 192 newAbsoluteDir = newAbsoluteDir + token.nextToken() + "/"; 193 } 194 195 String newRelativeFile = relativeFile.substring(3, relativeFile.length()); 196 fileName = fileName(newAbsoluteDir, newRelativeFile); 197 } else if (relativeFile.indexOf("./") == 0) { 198 fileName = newAbsoluteDir + relativeFile.substring(2, relativeFile.length()); 199 } else { 200 fileName = newAbsoluteDir + relativeFile; 201 } 202 203 return fileName; 204 } 205 206 215 public static String concat(String absoluteFile, String relativeFile) { 216 File file = new File (absoluteFile); 217 218 if (file.isFile()) { 219 return fileName(file.getParent(), relativeFile); 220 } 221 222 return fileName(absoluteFile, relativeFile); 223 } 224 225 233 public static void deleteParentDirs(File start, File stop) throws IllegalArgumentException { 234 if (!stop.isDirectory()) 235 throw new IllegalArgumentException ("Stop dir '" + stop.getAbsolutePath() 236 + "' is not a directory"); 237 if (!start.getAbsolutePath().startsWith(stop.getAbsolutePath())) 238 throw new IllegalArgumentException ("Start dir '" + start.getAbsolutePath() 239 + "' is not a descending sibling of stop directory '" + stop.getAbsolutePath() 240 + "'."); 241 242 File parent = start.getParentFile(); 243 244 while (!parent.equals(stop) && parent.delete()) 245 parent = parent.getParentFile(); 246 } 247 } | Popular Tags |