1 21 package com.db4o.foundation.io; 22 23 import java.io.*; 24 25 28 public class File4 { 29 30 public static void rename(String oldPath,String newPath) throws IOException { 31 if(!new java.io.File (oldPath).renameTo(new File(newPath))) { 32 throw new IOException("Could not rename '"+oldPath+"' to '"+newPath+"'."); 33 } 34 } 35 36 public static void copy(String source, String target) { 37 try { 38 java.io.File sourceFile = new java.io.File (source); 39 40 java.io.File targetFile = new java.io.File (target); 41 targetFile.mkdirs(); 42 targetFile.delete(); 43 44 if (sourceFile.isDirectory()) { 45 copyDirectory(sourceFile, targetFile); 46 } else { 47 copyFile(sourceFile, targetFile); 48 } 49 } catch (Exception e) { 50 System.out.println("File.copy failed."); 51 e.printStackTrace(); 52 throw new RuntimeException (); 53 } 54 } 55 56 public static void copyFile(File source, File target) throws IOException { 57 final int bufferSize = 64000; 58 59 RandomAccessFile rafIn = new RandomAccessFile(source.getAbsolutePath(), "r"); 60 RandomAccessFile rafOut = new RandomAccessFile(target.getAbsolutePath(), "rw"); 61 long len = rafIn.length(); 62 byte[] bytes = new byte[bufferSize]; 63 64 while (len > 0) { 65 len -= bufferSize; 66 if (len < 0) { 67 bytes = new byte[(int) (len + bufferSize)]; 68 } 69 rafIn.read(bytes); 70 rafOut.write(bytes); 71 } 72 73 rafIn.close(); 74 rafOut.close(); 75 } 76 77 private static void copyDirectory(File source, File target) { 78 String [] files = source.list(); 79 if (files != null) { 80 for (int i = 0; i < files.length; i++) { 81 copy(Path4.combine(source.getAbsolutePath(), files[i]), 82 Path4.combine(target.getAbsolutePath(), files[i])); 83 } 84 } 85 } 86 87 public static void delete(String fname) { 88 new File(fname).delete(); 89 } 90 91 public static boolean exists(String fname){ 92 return new File(fname).exists(); 93 } 94 95 public static void mkdirs(String path) { 96 new File(path).mkdirs(); 97 } 98 } 99 | Popular Tags |