java 操作文件夾代碼

engd 9年前發布 | 2K 次閱讀 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();  
        }  
    }  
}  

} </pre>

 本文由用戶 engd 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!