package some.package;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* 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);
}

outBuffer.close();
inBuffer.close();
out.close();
in.close();

// cleanupif files are not the same length
if (fromFile.length() != toFile.length())
{
toFile.delete();

return false;
}

return true;
}
catch (IOException e)
{
return false;
}
}

/** 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();
}

return false;
}
}


http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4073756