Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 1.84 KB  |  views: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  //Change renameTo to work like this
  2.     @Override
  3.     public boolean renameTo(File dest) {
  4.         try {
  5.             return move(this, dest);
  6.         } catch (SecurityException ex) {
  7.             return false;
  8.         }
  9.     }
  10.  
  11.  
  12.  
  13.  
  14.  
  15.         /** Copy a File
  16.         * The renameTo method does not allow action across NFS mounted filesystems
  17.         * this method is the workaround
  18.         *
  19.         * @param fromFile The existing File
  20.         * @param toFile The new File
  21.         * @return  <code>true</code> if and only if the renaming succeeded;
  22.         *          <code>false</code> otherwise
  23.         */
  24.          private final static boolean copy (File fromFile, File toFile)
  25.          {
  26.                 try
  27.                 {
  28.                         FileInputStream in = new FileInputStream(fromFile);
  29.                         FileOutputStream out = new FileOutputStream(toFile);
  30.                         BufferedInputStream inBuffer = new BufferedInputStream(in);
  31.                         BufferedOutputStream outBuffer = new BufferedOutputStream(out);
  32.  
  33.                         int theByte = 0;
  34.  
  35.                         while ((theByte = inBuffer.read()) > -1)
  36.                         {
  37.                           outBuffer.write(theByte);
  38.                         }
  39.  
  40.                         outBuffer.close();
  41.                         inBuffer.close();
  42.                         out.close();
  43.                         in.close();
  44.  
  45.                         // cleanupif files are not the same length
  46.                         if (fromFile.length() != toFile.length())
  47.                         {
  48.                           toFile.delete();
  49.  
  50.                           return false;
  51.                         }
  52.  
  53.                         return true;
  54.                 }
  55.                 catch (IOException e)
  56.                 {
  57.                         return false;
  58.                 }
  59.         }
  60.  
  61.   /** Move a File
  62.     * The renameTo method does not allow action across NFS mounted filesystems
  63.     * this method is the workaround
  64.     *
  65.     * @param fromFile The existing File
  66.     * @param toFile The new File
  67.     * @return  <code>true</code> if and only if the renaming succeeded;
  68.     *          <code>false</code> otherwise
  69.     */
  70.         public final static boolean move (File fromFile, File toFile)
  71.         {
  72.                  if(fromFile.renameTo(toFile))
  73.                  {
  74.                    return true;
  75.                  }
  76.  
  77.                  // delete if copy was successful, otherwise move will fail
  78.                  if (copy(fromFile, toFile))
  79.                  {
  80.                    return fromFile.delete();
  81.                  }
  82.  
  83.                  return false;
  84.         }