java文件

一.File类

371e5086178c4a8a91663f29d65c67ed.png

63990d0c51ae4b0aa1044d486e8d2a16.jpg

 

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

我的代码:

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();
            }
        }
    }
}

答案代码:

 

/**
 * 扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件
 *
 * @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());
                }
            }
        }
    }
}

三.进行普通文件的复制

我的代码:

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);
                }
            }
        }
    }
}
/**
 * 进行普通文件的复制
 * 
 * @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();
                }
            }
        }
    }
}

 

答案代码: 

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

我的代码:

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;
                }
            }
        }
    }
}

答案代码: 

 

/**
 * 扫描指定目录,并找到名称或者内容中包含指定字符的所有普通文件(不包含目录)
 *
 * @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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数九天有一个秘密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值