中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java 操作文件夾代碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
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方法將文件夾全部復(fù)制后再刪除整個(gè)文件夾,這種方法不好。因?yàn)槿绻袔讉(gè)文件復(fù)制失敗,源文件也會(huì)都被刪除了  

//若要剪切應(yīng)該用下面的copy方法,將參數(shù)cut設(shè)為true,它是復(fù)制一個(gè)刪除一個(gè),復(fù)制失敗就不會(huì)刪除源文件  

    /**  
     *   
     * @param src:源文件(夾)  
     * @param dst:目標(biāo)文件(夾)  
     * @param forced:如果遇到同名文件,是否覆蓋  
     * @param cut:復(fù)制完是否刪除源文件,若設(shè)為true,效果如同剪切  
         * 注意:dst是目標(biāo)路徑而不是目標(biāo)路徑所在的文件夾,比如要不c:\dir復(fù)制到d:\盤(pán)下,則dst應(yīng)該是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();  
            }  
        }  
    }  
} 

標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:PHP高亮關(guān)鍵詞

下一篇:PHP讀取遠(yuǎn)程文件