• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

java 文件夹操作

JAVA相关 水墨上仙 1830次浏览

java 文件夹操作


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, dst,true,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();  
            }  
        }  
    }  
} 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明java 文件夹操作
喜欢 (0)
加载中……