java文件

一.File类

二.扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件

我的代码:

java 复制代码
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Test1 {
    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) throws IOException {
        System.out.println("请输入目标目录:");
        File rootPath = new File(scanner.next());
        if (!rootPath.isDirectory()) {
            System.out.println("目标目录错误");
            return;
        }
        System.out.println("请输入关键字");
        String word = scanner.next();

        fingDir(rootPath, word);
    }

    private static void fingDir(File rootPath, String word) throws IOException {
        File[] files = rootPath.listFiles();

        if (files == null || files.length == 0) {
            return;
        }

        for (int i = 0; i < files.length; i++) {
            System.out.println(files[i].getCanonicalPath());
            if (files[i].isFile()) {
                delFile(files[i], word);
            } else {
                fingDir(files[i], word);
            }
        }
    }

    private static void delFile(File file, String word) {
        if (file.getName().contains(word)) {
            System.out.println("找到了,是否删除y/n");
            if (scanner.next().equals("y")) {
                file.delete();
            }
        }
    }
}

答案代码:

java 复制代码
/**
 * 扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件
 *
 * @Author 比特就业课
 * @Date 2022-06-29
 */
public class file_2445 {
    public static void main(String[] args) {
        // 接收用户输入的路径
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要扫描的目录:");
        String rootPath = scanner.next();
        if (rootPath == null || rootPath.equals("")) {
            System.out.println("目录不能为空。");
            return;
        }
        // 根据目录创建File对象
        File rootDir = new File(rootPath);
        if (rootDir.isDirectory() == false) {
            System.out.println("输入的不是一个目录,请检查!");
            return;
        }

        // 接收查找条件
        System.out.println("请输入要找出文件名中含的字符串:");
        String token = scanner.next();
        // 用于存储符合条件的文件
        List<File> fileList = new ArrayList<>();
        // 开始查找
        scanDir(rootDir, token, fileList);
        // 处理查找结果
        if (fileList.size() == 0) {
            System.out.println("没有找到符合条件的文件。");
            return;
        }
        for (File file : fileList) {
            System.out.println("请问您要删除文件" + file.getAbsolutePath() + "吗?(y/n)");
            String order = scanner.next();
            if (order.equals("y")) {
                file.delete();
            }
        }
    }

    private static void scanDir(File rootDir, String token, List<File> fileList) {
        File[] files = rootDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }
        // 开始查找
        for (File file:files) {
            if (file.isDirectory()) {
                // 如果是一个目录就递归查找子目录
                scanDir(file, token, fileList);
            } else {
                // 如果是符合条件的文件就记录
                System.out.println(token);
                if (file.getName().contains(token)) {
                    fileList.add(file.getAbsoluteFile());
                }
            }
        }
    }
}

三.进行普通文件的复制

我的代码:

java 复制代码
import java.io.*;
import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入目标文件的路径");
        File file1 = new File(scanner.next());
        if (!file1.isFile()) {
            System.out.println("目标文件错误");
            return;
        }
        System.out.println("请输入新文件的路径");
        File file2 = new File(scanner.next());
        if (!file2.getParentFile().isDirectory()) {
            System.out.println("新文件路径错误");
            return;
        }

        copyFile(file1, file2);
    }

    private static void copyFile(File file1, File file2) throws IOException {
        try(InputStream inputStream = new FileInputStream(file1);OutputStream outputStream = new FileOutputStream(file2)) {
            while (true) {
                byte[] buffer = new byte[2048];
                int n = inputStream.read(buffer);
                System.out.println(n);
                if (n == -1) {
                    System.out.println("结束");
                    break;
                } else {
                    outputStream.write(buffer, 0, n);
                }
            }
        }
    }
}
java 复制代码
/**
 * 进行普通文件的复制
 * 
 * @Author 比特就业课
 * @Date 2022-06-29
 */
public class File_2446 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 接收源文件目录
        System.out.println("请输入源文件的路径:");
        String sourcePath = scanner.next();
        if (sourcePath == null || sourcePath.equals("")) {
            System.out.println("源文路径不能为空。");
            return;
        }
        // 实例源文件
        File sourceFile = new File(sourcePath);
        // 校验合法性
        // 源文件不存在
        if (!sourceFile.exists()) {
            System.out.println("输入的源文件不存在,请检查。");
            return;
        }
        // 源路径对应的是一个目录
        if (sourceFile.isDirectory()) {
            System.out.println("输入的源文件是一个目录,请检查。");
            return;
        }

        // 输入目标路径
        System.out.println("请输入目标路径:");
        String destPath = scanner.next();
        if (destPath == null || destPath.equals("")) {
            System.out.println("目标路径不能为空。");
            return;
        }
        File destFile = new File(destPath);
        // 检查目标路径合法性
        // 已存在
        if (destFile.exists()) {
            // 是一个目录
            if (destFile.isDirectory()) {
                System.out.println("输入的目标路径是一个目录,请检查。");
            }
            // 是一个文件
            if (destFile.isFile()) {
                System.out.println("文件已存在,是否覆盖,y/n?");
                String input = scanner.next();
                if (input != null && input.toLowerCase().equals("")) {
                    System.out.println("停止复制。");
                    return;
                }
            }
        }

        // 复制过程

        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // 1. 读取源文件
            inputStream = new FileInputStream(sourceFile);
            // 2. 输出流
            outputStream = new FileOutputStream(destFile);
            // 定义一个缓冲区
            byte[] byes = new byte[1024];
            int length;
            while (true) {
                // 获取读取到的长度
                length = inputStream.read(byes);
                // 值为-1表示没有数据读出
                if (length == -1) {
                    break;
                }
                // 把读到的length个字节写入到输出流
                outputStream.write(byes, 0, length);
            }
            // 将输出流中的数据写入文件
            outputStream.flush();
            System.out.println("复制成功。" + destFile.getAbsolutePath());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭输入流
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 关闭输出流
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

答案代码:

四. 扫描指定目录,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)

我的代码:

java 复制代码
import java.io.*;
import java.util.Scanner;

public class Test3 {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        System.out.println("请输入路径");
        File rootDir = new File(scanner.next());
        if (!rootDir.isDirectory()) {
            System.out.println("目录输入错误");
            return;
        }
        System.out.println("请输入名称");
        String word = scanner.next();

        findFile(rootDir, word);
    }

    private static void findFile(File rootDir, String word) throws IOException {
        File[] files = rootDir.listFiles();

        if (files == null || files.length == 0) {
            return;
        }

        for (File f : files) {
            if (f.isFile()) {
                isFind(f, word);
            } else {
                findFile(f, word);
            }
        }
    }

    private static void isFind(File f, String word) throws IOException {
        if (f.getName().contains(word)) {
            System.out.println("找到了" + f.getPath());
        } else {
            try (Reader reader = new FileReader(f)) {
                Scanner scanner1 = new Scanner(reader);
                String in = scanner1.nextLine();
                if (in.contains(word)) {
                    System.out.println("找到了" + f.getPath());
                } else {
                    return;
                }
            }
        }
    }
}

答案代码:

java 复制代码
/**
 * 扫描指定目录,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)
 *
 * @Author 比特就业课
 * @Date 2022-06-28
 */
public class File_2447 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        // 接收用户输入的路径
        System.out.println("请输入要扫描的路径:");
        String rootPath = scanner.next();
        // 校验路径合法性
        if (rootPath == null || rootPath.equals("")) {
            System.out.println("路径不能为空。");
            return;
        }
        // 根据输入的路径实例化文件对象
        File rootDir = new File(rootPath);
        if (rootDir.exists() == false) {
            System.out.println("指定的目录不存在,请检查。");
            return;
        }
        if (rootDir.isDirectory() == false) {
            System.out.println("指定的路径不是一个目录。请检查。");
            return;
        }

        // 接收要查找的关键字
        System.out.println("请输入要查找的关键字:");
        String token = scanner.next();
        if (token == null || token.equals("")) {
            System.out.println("查找的关键字不能为空,请检查。");
            return;
        }

        // 遍历目录查找符合条件的文件
        // 保存找到的文件
        List<File> fileList = new ArrayList<>();
        scanDir(rootDir, token, fileList);

        // 打印结果
        if (fileList.size() > 0) {
            System.out.println("共找到了 " + fileList.size() + "个文件:");
            for (File file: fileList) {
                System.out.println(file.getAbsoluteFile());
            }
        } else {
            System.out.println("没有找到相应的文件。");
        }

    }

    private static void scanDir(File rootDir, String token, List<File> fileList) throws IOException {
        // 获取目录下的所有文件
        File[] files = rootDir.listFiles();
        if (files == null || files.length == 0) {
            return;
        }

        // 遍历
        for (File file : files) {
            if (file.isDirectory()) {
                // 如果是文件就递归
                scanDir(file, token, fileList);
            } else {
                // 文件名是否包含
                if (file.getName().contains(token)) {
                    fileList.add(file);
                } else {
                    if (isContainContent(file, token)) {
                        fileList.add(file.getAbsoluteFile());
                    }
                }

            }
        }
    }

    private static boolean isContainContent(File file, String token) throws IOException {
        // 定义一个StringBuffer存储读取到的内容
        StringBuffer sb = new StringBuffer();
        // 输入流
        try (InputStream inputStream = new FileInputStream(file)) {
            try (Scanner scanner = new Scanner(inputStream, "UTF-8")) {
                // 读取每一行
                while (scanner.hasNext()) {
                    // 一次读一行
                    sb.append(scanner.nextLine());
                    // 加入一行的结束符
                    sb.append("\r\n");
                }
            }
        }
        return sb.indexOf(token) != -1;
    }
}
相关推荐
考虑考虑4 小时前
Jpa使用union all
java·spring boot·后端
用户3721574261354 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊5 小时前
Java学习第22天 - 云原生与容器化
java
渣哥7 小时前
原来 Java 里线程安全集合有这么多种
java
间彧7 小时前
Spring Boot集成Spring Security完整指南
java
间彧7 小时前
Spring Secutiy基本原理及工作流程
java
Java水解8 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆10 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试
前端小张同学11 小时前
服务器上如何搭建jenkins 服务CI/CD😎😎
java·后端
ytadpole11 小时前
Spring Cloud Gateway:一次不规范 URL 引发的路由转发404问题排查
java·后端