国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java遞歸實現操作系統文件的復制、粘貼和刪除功能

2019-11-14 15:18:22
字體:
來源:轉載
供稿:網友

通過java IO遞歸實現操作系統對文件的復制、粘貼和刪除功能,剪切=復制+粘貼+刪除

代碼示例:

import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/** * achieve file copy and delete using recursion *  * @author ForeverLover * */public class FileCopyAndDelete {    // delete files of this path    public void deleteFile(String path) {        File f = new File(path);        if (f.isDirectory()) {            File[] file = f.listFiles();            for (File file1 : file) {                this.deleteFile(file1.toString());                file1.delete();            }        } else {            f.delete();        }        f.delete();    }    // copy files from path1 to path2    public void copyFiles(String path1, String path2) throws IOException {        File f = new File(path1);        if (f.isDirectory()) {            File file = new File(path2);            if (!file.exists())                file.mkdir();            File[] file1 = f.listFiles();            for (File file2 : file1) {                copyFiles(file2.toString(), path2 + "/" + file2.getName());            }        } else {            copy(path1, path2);        }    }    // copy file from path1 to path2 one by one    public void copy(String path1, String path2) throws IOException {        DataInputStream in = new DataInputStream(new BufferedInputStream(                new FileInputStream(path1)));        byte[] b = new byte[in.available()];// available返回實際可讀字節數,即總大小        in.read(b);        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(                new FileOutputStream(path2)));        out.write(b);        in.close();        out.close();    }    //main method    public static void main(String[] args) {        FileCopyAndDelete f = new FileCopyAndDelete();        // test copy files using recursive        /*         * { String path1 = "D://Folder1"; String path2 = "D://Folder2"; try {         * f.copyFiles(path1, path2); System.out.*/        // test delete files using recursive        /*         * { f.deleteFile("C://Folder1");         * System.out.println("OK,DELETE FINISH"); }         */    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 惠州市| 仪征市| 荥经县| 宁晋县| 南漳县| 东乌| 乌兰浩特市| 冕宁县| 和林格尔县| 临泉县| 龙泉市| 柯坪县| 横山县| 多伦县| 龙陵县| 大化| 芒康县| 铜川市| 江门市| 封丘县| 墨玉县| 长寿区| 突泉县| 南京市| 泸水县| 通化市| 上蔡县| 个旧市| 鄂伦春自治旗| 班戈县| 阳江市| 仁化县| 永福县| 钟山县| 金寨县| 依安县| 北碚区| 新源县| 永泰县| 松溪县| 桦川县|