国产探花免费观看_亚洲丰满少妇自慰呻吟_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"); }         */    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东乌珠穆沁旗| 扶余县| 友谊县| 西乡县| 巨野县| 筠连县| 称多县| 平阴县| 巨野县| 金昌市| 灯塔市| 南昌县| 静宁县| 响水县| 云阳县| 齐齐哈尔市| 古交市| 唐河县| 宜宾市| 册亨县| 永寿县| 边坝县| 竹山县| 南乐县| 乐清市| 囊谦县| 新津县| 萝北县| 玉溪市| 永新县| 平谷区| 乐业县| 东光县| 古田县| 开江县| 长沙市| 瓮安县| 吉安市| 肃宁县| 大新县| 介休市|