/** * This class contains static methods that perform operations on files * Due to limitations of File objects across NFS mounted filesystems * this class provides a workaround * */
public final class FileUtils { /** Private constructor to prevent instantiation. * All of the methods of this class are static. */ private FileUtils() { //prevent instantiation; }
/** Copy a File * The renameTo method does not allow action across NFS mounted filesystems * this method is the workaround * * @param fromFile The existing File * @param toFile The new File * @return <code>true</code> if and only if the renaming succeeded; * <code>false</code> otherwise */ public final static boolean copy (File fromFile, File toFile) { try { FileInputStream in = new FileInputStream(fromFile); FileOutputStream out = new FileOutputStream(toFile); BufferedInputStream inBuffer = new BufferedInputStream (in); BufferedOutputStream outBuffer = new BufferedOutputStream(out);
int theByte = 0;
while ((theByte = inBuffer.read()) > -1) { outBuffer.write(theByte); }
/** Move a File * The renameTo method does not allow action across NFS mounted filesystems * this method is the workaround * * @param fromFile The existing File * @param toFile The new File * @return <code>true</code> if and only if the renaming succeeded; * <code>false</code> otherwise */ public final static boolean move (File fromFile, File toFile) { if(fromFile.renameTo(toFile)) { return true; }
// delete if copy was successful, otherwise move will fail if (copy(fromFile, toFile)) { return fromFile.delete(); }