• Java递归实现操作系统文件的复制、粘贴和删除功能


    通过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.println("OK,COPY FINISH"); }
             * catch (IOException e) { e.printStackTrace(); } }
             */
    
            // test delete files using recursive
            /*
             * { f.deleteFile("C://Folder1");
             * System.out.println("OK,DELETE FINISH"); }
             */
    
        }
    }
  • 相关阅读:
    LINUX-----管道流及重定向
    转:vim----复制粘贴
    linux ----虚拟机无法与本地机通信
    C语言---注释
    C语言---翻译过程
    DROP--删除表
    ALTER---删除字段
    Oracle中用一条Sql实现任意的行转列拼接 多行拼接
    Oracle列操作(增加列,修改列,删除列)
    Java 把long 转换成 日期 再转换成String类型
  • 原文地址:https://www.cnblogs.com/ForeverLover/p/4910757.html
Copyright © 2020-2023  润新知