[Java] java 文件夹操作 →→→→→进入此内容的聊天室

来自 , 2019-03-19, 写在 Java, 查看 130 次.
URL http://www.code666.cn/view/298f5874
  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.nio.ByteBuffer;  
  6. import java.nio.channels.FileChannel;  
  7.  
  8. public class FileManipulationTest {  
  9.  
  10.     public static void main(String[] args) {  
  11.         // TODO Auto-generated method stub  
  12.         File src = new File("C:/Users/leo/Desktop/yidu");  
  13.         File dst = new File("C:/Users/leo/Desktop/dd/mm");  
  14.         copy(src, dst,true,true);//剪切  
  15.     }  
  16.  
  17.  
  18.     public static boolean rename(File file,String name){  
  19.         String str = file.getParent();  
  20.         if(!str.endsWith(File.separator))  
  21.             str += File.separator;  
  22.         return file.renameTo(new File(str+name));  
  23.     }  
  24.  
  25.  
  26.     public static void delete(File file){  
  27.         if(file.isFile()){  
  28.             file.delete();  
  29.         }else if(file.isDirectory()){  
  30.             File[] list = file.listFiles();  
  31.             for(int i=0;i<list.length;i++){
  32.                 delete(list[i]);                  
  33.             }  
  34.             file.delete();  
  35.         }  
  36.     }  
  37.  
  38.     @Deprecated
  39.     public static void cut(File src,File dst){  
  40.         copy(src, dst,true,false);  
  41.         delete(src);  
  42.  
  43.     }  
  44.  
  45. //上面的cut方法将文件夹全部复制后再删除整个文件夹,这种方法不好。因为如果有几个文件复制失败,源文件也会都被删除了  
  46.  
  47. //若要剪切应该用下面的copy方法,将参数cut设为true,它是复制一个删除一个,复制失败就不会删除源文件  
  48.  
  49.     /**  
  50.      *  
  51.      * @param src:源文件(夹)  
  52.      * @param dst:目标文件(夹)  
  53.      * @param forced:如果遇到同名文件,是否覆盖  
  54.      * @param cut:复制完是否删除源文件,若设为true,效果如同剪切  
  55.          * 注意:dst是目标路径而不是目标路径所在的文件夹,比如要不c:\dir复制到d:\盘下,则dst应该是File("d:\dir")而不是File("d:\")  
  56.      */  
  57.  
  58.     public static void copy(File src,File dst,boolean forced,boolean cut){  
  59.         if(src.isFile()){  
  60.             if(!dst.isFile() || forced)  
  61.                 try {  
  62.                     dst.createNewFile();  
  63.                     _copy(src, dst);  
  64.                     if(cut)  
  65.                         src.delete();  
  66.                 } catch (IOException e) {  
  67.                     // TODO Auto-generated catch block  
  68.                     e.printStackTrace();  
  69.                 }            
  70.         }  
  71.         else if(src.isDirectory()){  
  72.             dst.mkdirs();  
  73.             File[] list = src.listFiles();  
  74.             for(int i=0;i<list.length;i++){  
  75.                 String rp = list[i].getAbsolutePath().substring(src.getAbsolutePath().length(), list[i].getAbsolutePath().length());  
  76.                 File dstFile = new File(dst.getAbsolutePath()+rp);  
  77.                 copy( list[i],dstFile,forced,cut);                
  78.             }  
  79.             if(cut)  
  80.                 src.delete();  
  81.         }  
  82.     }  
  83.  
  84.     private static void _copy(File src,File dst) throws IOException{  
  85.         FileChannel dstfc = null;  
  86.         FileChannel srcfc = null;  
  87.         try {  
  88.             dstfc = new FileOutputStream(dst).getChannel();  
  89.             srcfc = new FileInputStream(src).getChannel();  
  90.             ByteBuffer buf = ByteBuffer.allocate(4*1024);  
  91.             while(srcfc.size()>srcfc.position()){  
  92.                 buf.clear();  
  93.                 srcfc.read(buf);  
  94.                 buf.flip();  
  95.                 dstfc.write(buf);  
  96.             }  
  97.         } finally {  
  98.             try {  
  99.                 if(dstfc != null)  
  100.                     dstfc.close();  
  101.             } catch (IOException e) {  
  102.                 // TODO Auto-generated catch block  
  103.                 e.printStackTrace();  
  104.             }  
  105.             try {  
  106.                 if(srcfc != null)  
  107.                     srcfc.close();  
  108.             } catch (IOException e) {  
  109.                 // TODO Auto-generated catch block  
  110.                 e.printStackTrace();  
  111.             }  
  112.         }  
  113.     }  
  114. }
  115.  
  116. //java/1340

回复 "java 文件夹操作"

这儿你可以回复上面这条便签

captcha