1 21 22 package org.apache.derbyTesting.functionTests.util; 23 24 import java.io.FileWriter ; 25 import java.io.File ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import java.security.PrivilegedActionException ; 29 import java.security.PrivilegedExceptionAction ; 30 31 35 public class FTFileUtil 36 { 37 44 public static void mkFile(String fileName, int length) throws Exception 45 { 46 FileWriter fw = new FileWriter (fileName); 47 int offset = 0; 48 String data = "Amber!"; 49 for (int ix=0;ix<length;ix++) 50 { 51 fw.write(data,offset,1); 52 offset++; 53 if (offset >= data.length()) offset = 0; 54 } 55 fw.close(); 56 } 57 58 67 public static void renameFile(String location, String name , 68 String newName) throws Exception 69 { 70 final File src = new File (location, name); 71 final File dst = new File (location, newName); 72 73 AccessController.doPrivileged(new PrivilegedExceptionAction () { 78 public Object run() throws Exception { 79 if(!src.renameTo(dst)) 80 { 81 throw new Exception ("unable to rename File: " + 82 src.getAbsolutePath() + 83 " To: " + dst.getAbsolutePath()); 84 } 85 86 return null; } 88 }); 89 } 90 91 92 103 public static String fileExists(String fileName) 104 throws PrivilegedActionException 105 { 106 final File fl = new File (fileName); 107 108 113 return (String ) 114 AccessController.doPrivileged(new PrivilegedExceptionAction () { 115 public Object run() 116 { 117 if(fl.exists()) { 118 return "true"; 119 }else { 120 return "false"; 121 } 122 } 123 }); 124 } 125 126 127 135 136 private static boolean removeDirectory(File directory) { 137 138 if (directory == null) 139 return false; 140 if (!directory.exists()) 141 return true; 142 if (!directory.isDirectory()) 143 return false; 144 145 String [] list = directory.list(); 146 147 if (list != null) { 148 for (int i = 0; i < list.length; i++) { 149 File entry = new File (directory, list[i]); 150 151 if (entry.isDirectory()) 152 { 153 if (!removeDirectory(entry)) 154 return false; 155 } 156 else 157 { 158 if (!entry.delete()) 159 return false; 160 } 161 } 162 } 163 164 return directory.delete(); 165 } 166 167 177 178 public static String removeDirectory(final String directory) 179 throws PrivilegedActionException 180 { 181 186 return (String ) 187 AccessController.doPrivileged(new PrivilegedExceptionAction () { 188 public Object run() 189 { 190 return (removeDirectory( 191 new File (directory)) ? "true" : "false"); 192 } 193 }); 194 } 195 196 } 197 198 199 200 | Popular Tags |