加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java 操作文件夹代码

发布时间:2020-12-15 00:22:51 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.nio.ByteBuffer;  
import java.nio.channels.FileChannel;  

public class FileManipulationTest {  

    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        File src = new File("C:/Users/leo/Desktop/yidu");  
        File dst = new File("C:/Users/leo/Desktop/dd/mm");  
        copy(src,dst,true,true);//剪切  
    }  


    public static boolean rename(File file,String name){  
        String str = file.getParent();  
        if(!str.endsWith(File.separator))  
            str += File.separator;  
        return file.renameTo(new File(str+name));  
    }  


    public static void delete(File file){  
        if(file.isFile()){  
            file.delete();  
        }else if(file.isDirectory()){  
            File[] list = file.listFiles();  
            for(int i=0;i<list.length;i++){ 
                delete(list[i]);                  
            }  
            file.delete();  
        }  
    }  

    @Deprecated 
    public static void cut(File src,File dst){  
        copy(src,false);  
        delete(src);  

    }  

//上面的cut方法将文件夹全部复制后再删除整个文件夹,这种方法不好。因为如果有几个文件复制失败,源文件也会都被删除了  

//若要剪切应该用下面的copy方法,将参数cut设为true,它是复制一个删除一个,复制失败就不会删除源文件  

    /**  
     *   
     * @param src:源文件(夹)  
     * @param dst:目标文件(夹)  
     * @param forced:如果遇到同名文件,是否覆盖  
     * @param cut:复制完是否删除源文件,若设为true,效果如同剪切  
         * 注意:dst是目标路径而不是目标路径所在的文件夹,比如要不c:dir复制到d:盘下,则dst应该是File("d:dir")而不是File("d:")  
     */  

    public static void copy(File src,File dst,boolean forced,boolean cut){  
        if(src.isFile()){  
            if(!dst.isFile() || forced)  
                try {  
                    dst.createNewFile();  
                    _copy(src,dst);  
                    if(cut)  
                        src.delete();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }             
        }  
        else if(src.isDirectory()){  
            dst.mkdirs();  
            File[] list = src.listFiles();  
            for(int i=0;i<list.length;i++){  
                String rp = list[i].getAbsolutePath().substring(src.getAbsolutePath().length(),list[i].getAbsolutePath().length());  
                File dstFile = new File(dst.getAbsolutePath()+rp);  
                copy( list[i],dstFile,forced,cut);                
            }  
            if(cut)  
                src.delete();  
        }  
    }  

    private static void _copy(File src,File dst) throws IOException{  
        FileChannel dstfc = null;  
        FileChannel srcfc = null;  
        try {  
            dstfc = new FileOutputStream(dst).getChannel();  
            srcfc = new FileInputStream(src).getChannel();  
            ByteBuffer buf = ByteBuffer.allocate(4*1024);  
            while(srcfc.size()>srcfc.position()){  
                buf.clear();  
                srcfc.read(buf);  
                buf.flip();  
                dstfc.write(buf);  
            }  
        } finally {  
            try {  
                if(dstfc != null)  
                    dstfc.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            try {  
                if(srcfc != null)  
                    srcfc.close();  
            } catch (IOException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
        }  
    }  
} 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读