test/java/net/URLClassLoader/closetest/Common.java
Print this page
@@ -21,10 +21,12 @@
* questions.
*/
import java.io.*;
import java.net.*;
+import java.nio.file.NoSuchFileException;
+import jdk.testlibrary.FileUtils;
public class Common {
static void copyFile (String src, String dst) {
copyFile (new File(src), new File(dst));
@@ -37,11 +39,13 @@
static void copyFile (File src, File dst) {
try {
if (!src.isFile()) {
throw new RuntimeException ("File not found: " + src.toString());
}
- dst.delete();
+ // delete dst, if it exists
+ try { FileUtils.delete(dst.toPath()); }
+ catch (NoSuchFileException x) {}
dst.createNewFile();
FileInputStream i = new FileInputStream (src);
FileOutputStream o = new FileOutputStream (dst);
byte[] buf = new byte [1024];
int count;
@@ -48,32 +52,23 @@
while ((count=i.read(buf)) >= 0) {
o.write (buf, 0, count);
}
i.close();
o.close();
- } catch (IOException e) {
+ } catch (IOException | InterruptedException e) {
throw new RuntimeException (e);
}
}
- static void rm_minus_rf (File path) {
+ static void rm_minus_rf (File path) throws IOException, InterruptedException {
if (!path.exists()) {
return;
}
if (path.isFile()) {
- if (!path.delete()) {
- throw new RuntimeException ("Could not delete " + path);
- }
+ FileUtils.delete(path.toPath());
} else if (path.isDirectory ()) {
- String[] names = path.list();
- File[] files = path.listFiles();
- for (int i=0; i<files.length; i++) {
- rm_minus_rf (new File(path, names[i]));
- }
- if (!path.delete()) {
- throw new RuntimeException ("Could not delete " + path);
- }
+ FileUtils.deleteTree(path.toPath());
} else {
throw new RuntimeException ("Trying to delete something that isn't a file or a directory");
}
}