栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 文件操作案例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 文件操作案例

目录

案例一

案例二

案例三


案例一

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

public class Demo1 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入文件所在目录:");
        String rootDir = scanner.nextLine();
        File f = new File(rootDir);
        if (!f.isDirectory()) {
            System.out.println("输入的目录有误!");
            return;
        }
        int choice = 0;
        do {
            System.out.print("查找文件(1)      ");
            System.out.println("退出(0)");
            System.out.print("请选择:");
            choice = scanner.nextInt();
            scanner.nextLine();
            if (choice == 1) {
                System.out.print("请输入文件名中包含的关键字:");
                String keyWord = scanner.nextLine();
                List list = new ArrayList<>();
                scanDir(f, list, keyWord);
                for (File file:list) {
                    System.out.println(file.getCanonicalPath());
                    System.out.print("是否要删除该文件?(Y/N):");
                    while (true) {
                        String decision = scanner.nextLine();
                        if (decision.equals("Y") || decision.equals("y")) {
                            file.delete();
                            System.out.println("删除成功!");
                            break;
                        } else if (decision.equals("N") || decision.equals("n")) {
                            System.out.println("取消删除!");
                            break;
                        } else {
                            System.out.print("输入有误,请重新输入:");
                        }
                    }
                }
                System.out.println("本轮检索结束!");
            } else if (choice != 0){
                System.out.println("输入有误,请重新输入!");
            }

        } while (choice != 0);
    }
    public static void scanDir(File file, List res, String name) throws IOException {
        File[] files = file.listFiles();
        if (files == null || files.length == 0) return;
        for (File f:files) {
            if (f.isFile()) {
                if (f.getName().contains(name)) {
                    res.add(f);
                }
            } else {
                scanDir(f, res, name);
            }
        }
    }
}

案例二

进行普通文件的复制

public class Demo2 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要复制的源文件的路径:");
        String srcPath = scanner.nextLine();
        File srcFile = new File(srcPath);
        if (!srcFile.isFile()) {
            System.out.println("输入的路径有误!");
            return;
        }
        System.out.print("请输入复制后的文件路径:");
        String destPath = scanner.nextLine();
        File destFile = new File(destPath);
        if (destFile.exists()) {
            System.out.println("此目录下已有该文件,无法复制!");
            return;
        }
        if (!destFile.getParentFile().exists()) {
            destFile.getParentFile().mkdirs();
        }
        try (InputStream inputStream = new FileInputStream(srcFile)) {
            byte[] bytes = new byte[1024];
            OutputStream outputStream = new FileOutputStream(destFile);
            while (true) {
                int len = inputStream.read(bytes);
                if (len == -1) break;
                outputStream.write(bytes, 0, len);
            }
            System.out.println("复制成功!");
        }
    }
}

案例三

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

public class Demo3 {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入你要检索的文件目录路径:");
        String rootDir = scanner.nextLine();
        File file = new File(rootDir);
        if (!file.isDirectory()) {
            System.out.println("输入的路径有误!");
            return;
        }
        System.out.print("请输入文件内容中包含的关键字:");
        String keyWord = scanner.nextLine();
        List list = new ArrayList<>();
        scanContext(file, list, keyWord);
        for (File f:list) {
            System.out.println(f.getCanonicalPath());
        }
    }

    public static void scanContext(File file, List list, String keyWord) throws FileNotFoundException {
        File[] files = file.listFiles();
        if (files == null || files.length == 0) return;
        for (File f:files) {
            if (f.isFile()) {
                if (isContains(f, keyWord)) list.add(f);
            } else {
                scanContext(f, list, keyWord);
            }
        }
    }

    public static boolean isContains(File file, String keyWord) throws FileNotFoundException {
        StringBuilder stringBuilder = new StringBuilder();
        try (InputStream inputStream = new FileInputStream(file)) {
            try (Scanner scanner = new Scanner(inputStream, "utf-8")) {
                while (scanner.hasNextLine()) {
                    String str = scanner.nextLine();
                    stringBuilder.append(str);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.indexOf(keyWord) != -1;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/881406.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号