• File类操作文件的两个例题


    通过File类操作文件夹最主要的方法就是通过递归实现循环,再加上文件IO流的读取对文件内容进行操作

    1 从键盘输入接收文件夹路径,打印出该文件夹下所有的以 .java 为结尾的文件名

    /***
     * 从键盘输入接收一个文件夹路径,打印出该文件夹下所有的.java文件名
     */
    public class ShowFile {
    
        public static void main(String[] args) {
    
            //从键盘获取路径
            File dir = getDir();
            System.out.println(dir);
    
            //输出路径
            showFile(dir);
    
        }
    
        private static void showFile(File dir) {
            int index = 0;
            File[] files = dir.listFiles();
            for (File f : files) {
                if (f.isFile() && f.getName().endsWith(".java")) {
                    index += f.length();
                    System.out.println(f.getAbsolutePath());
                }else if (f.isDirectory()){
                    //传入的是一个目录
                    showFile(f);
                }
            }
            //接收文件夹路径并统计该文件夹的长度
            System.out.println("length=>" + index);
        }
    
        //从键盘获取一个有效的目录
        private static File getDir() {
    
            Scanner sc = new Scanner(System.in);
            System.out.println("输入");
            String path;
            while (true) {
                //获取路径
                path = sc.nextLine();
                File dir = new File(path);
                if (dir.exists() == false) {
                    //路径不存在
                    System.out.println("路径不存在");
                }else if (dir.isFile()) {
                    //普通文件
                    System.out.println("输入的是文件,不是文件名");
                }else {
                    return dir;
                }
            }
        }
    }
    
    

    2 复制一个文件夹下的所有文件到另一个文件夹下

    /***
     * 复制任意文件夹下面所有文件和子文件夹内容到D:/test。
     * 提示:涉及单个文件复制、目录的创建、递归的使用
     */
    public class TestWork5 {
    
        public static void main(String[] args) throws IOException {
            
            Scanner sc = new Scanner(System.in);
    
            String path = sc.nextLine();
            copyDirectory(path, "D:\test");
        }
    
        //复制目录的方法
        public static void copyDirectory( String sourceDirectory, String targetDirectory) throws IOException {
            //创建文件对象
            File sourceFile = new File(sourceDirectory);
            File targetFile = new File(targetDirectory);
            //判断源目录
            if(!sourceFile.exists() || !sourceFile.isDirectory()) {
                System.out.println("源目录不存在!!");
                return ;
            }
    
            //判断目标目录,若不存在,则手动创建
            if(!targetFile.exists()) {
                targetFile.mkdirs();
            }
    
            //获取源目录的所有文件和目录
            File[] sourceList = sourceFile.listFiles();
            System.out.println(sourceList.length);
            //遍历源目录
            for (File f : sourceList) {
                if(f.isFile()) {
                    //源文件
                    File sourceF = f;
                    //目标文件 父路径 + 子路径
                    File targetF = new File(targetFile,f.getName());
                    //调用复制文件的方法
                    copyFiles(sourceF, targetF);
                }
                //若为目录
                if(f.isDirectory()) {
                    //源目录
                    String sourceD = sourceDirectory + File.separator + f.getName();
                    System.out.println("sourceD: " + sourceD);
                    //目标目录
                    String targetD = targetDirectory + File.separator + f.getName();
                    System.out.println("targetD: " + targetD);
                    //递归调用复制目录的方法
                    copyDirectory(sourceD, targetD);
                }
            }
        }
    
        //复制文件的方法
        public static void copyFiles(File sourceFile, File targetFile) throws IOException {
    
            BufferedReader br = new BufferedReader(new FileReader(sourceFile));
            BufferedWriter bw = new BufferedWriter(new FileWriter(targetFile));
    
            //用字节流可能会出现数组越界的情况
            String line;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
            bw.flush();
            br.close();
            bw.close();
        }
    
    }
    
  • 相关阅读:
    Python_turtle绘图实例(持续更新)
    C++程序设计实验考试准备资料(2019级秋学期)
    利用next_permutation()实现全排列 完成 阮小二买彩票
    用埃氏算法来素数求和
    C++指针注意事项
    double与float的输入输出格式
    图片文件隐写术
    文件操作与隐写
    MFC 消息机制
    MFC应用中处理消息 创建窗口和会话框的顺序
  • 原文地址:https://www.cnblogs.com/xly1029/p/13943927.html
Copyright © 2020-2023  润新知