Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Change renameTo to work like this
- @Override
- public boolean renameTo(File dest) {
- try {
- return move(this, dest);
- } catch (SecurityException ex) {
- return false;
- }
- }
- /** 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
- */
- private 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;
- }
Add Comment
Please, Sign In to add comment