move file

 1     public static boolean move(String srcFile, String destPath) {
 2         // File (or directory) to be moved
 3         File file = new File(srcFile);
 4     
 5         // Destination directory
 6         File dir = new File(destPath);
 7     
 8         // Move file to new directory
 9         boolean success = file.renameTo(new File(dir, file.getName()));
10     
11         return success;
12     } 


     public static void copyFile(File sourceFile, File destFile) throws IOException {
         if(!destFile.exists()) {
             destFile.createNewFile();
         }

         FileChannel source = null;
         FileChannel destination = null;
         try {
             source = new FileInputStream(sourceFile).getChannel();
             destination = new FileOutputStream(destFile).getChannel();

             // previous code: destination.transferFrom(source, 0, source.size());
             // to avoid infinite loops, should be:
             long count = 0;
             long size = source.size();              
             while((count += destination.transferFrom(source, count, size-count))<size);
         }
         finally {
             if(source != null) {
                 source.close();
             }
             if(destination != null) {
                 destination.close();
             }
         }

 

 

posted on 2015-04-13 21:41  子墨sky  阅读(171)  评论(0)    收藏  举报

导航