1 2 3 |
package some.package;<br /><br />import java.io.BufferedInputStream;<br />import java.io.BufferedOutputStream;<br />import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.FileOutputStream;<br />import java.io.FileNotFoundException;<br />import java.io.IOException;<br /><br />/**<br /> * This class contains static methods that perform <br />operations on files<br /> * Due to limitations of File objects across NFS mounted <br />filesystems<br /> * this class provides a workaround<br /> *<br /> */<br /><br />public final class FileUtils<br />{<br /> /** Private constructor to prevent instantiation.<br /> * All of the methods of this class are static.<br /> */<br /> private FileUtils()<br /> {<br /> //prevent instantiation;<br /> }<br /><br /> /** Copy a File<br /> * The renameTo method does not allow action across NFS <br />mounted filesystems<br /> * this method is the workaround<br /> *<br /> * @param fromFile The existing File<br /> * @param toFile The new File<br /> * @return <code>true</code> if and only if the renaming <br />succeeded;<br /> * <code>false</code> otherwise<br /> */<br /> public final static boolean copy (File fromFile, File toFile)<br /> {<br /> try<br /> {<br /> FileInputStream in = new FileInputStream(fromFile);<br /> FileOutputStream out = new FileOutputStream(toFile);<br /> BufferedInputStream inBuffer = new BufferedInputStream<br />(in);<br /> BufferedOutputStream outBuffer = new <br />BufferedOutputStream(out);<br /><br /> int theByte = 0;<br /><br /> while ((theByte = inBuffer.read()) > -1)<br /> {<br /> outBuffer.write(theByte);<br /> }<br /><br /> outBuffer.close();<br /> inBuffer.close();<br /> out.close();<br /> in.close();<br /><br /> // cleanupif files are not the same length<br /> if (fromFile.length() != toFile.length())<br /> {<br /> toFile.delete();<br /> <br /> return false;<br /> }<br /> <br /> return true;<br /> }<br /> catch (IOException e)<br /> {<br /> return false;<br /> }<br /> }<br /><br /> /** Move a File<br /> * The renameTo method does not allow action across NFS <br />mounted filesystems<br /> * this method is the workaround<br /> *<br /> * @param fromFile The existing File<br /> * @param toFile The new File<br /> * @return <code>true</code> if and only if the renaming <br />succeeded;<br /> * <code>false</code> otherwise<br /> */<br /> public final static boolean move (File fromFile, File toFile)<br /> {<br /> if(fromFile.renameTo(toFile))<br /> {<br /> return true;<br /> }<br /><br /> // delete if copy was successful, otherwise move will fail<br /> if (copy(fromFile, toFile))<br /> {<br /> return fromFile.delete();<br /> }<br /><br /> return false;<br /> }<br />}<br /><br /><br />http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4073756<br /> |