济宁做网站,深圳网站品牌建设,wordpress 主题宽度,打开app登录文章目录 前言1. 案例12. 案例23. 案例3总结 前言
我们学会了文件的基本操作 文件内容读写操作#xff0c;接下来#xff0c;我们实现一些小工具程序#xff0c;来锻炼我们的能力。
关注收藏, 开始学习吧#x1f9d0; 1. 案例1
扫描指定目录#xff0c;并找到名称中包… 文章目录 前言1. 案例12. 案例23. 案例3总结 前言
我们学会了文件的基本操作 文件内容读写操作接下来我们实现一些小工具程序来锻炼我们的能力。
关注收藏, 开始学习吧 1. 案例1
扫描指定目录并找到名称中包含指定字符的所有普通文件不包含目录并且后续询问用户是否要删除该文件
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;/*
* 扫描指定目录
* 并找到名称中包含指定字符的所有普通文件不包含目录
* 并且后续询问用户是否要删除该文件
* */
public class Demo10 {public static void main(String[] args) throws IOException {Scanner scanner new Scanner(System.in);System.out.println(请输入您要搜索的根目录: );String rootPath scanner.nextLine();File rootDir new File(rootPath);if (!rootDir.isDirectory()) {System.out.println(您输入的文件不存在或者不是目录, 退出程序);return;}System.out.println(请输入您要搜索的关键词: );String word scanner.nextLine();ListFile result new ArrayList();scanDir(rootDir, word, result);System.out.println(搜索完成, 共搜索到 result.size() 个文件, 分别是);for (File file : result) {System.out.println(file.getCanonicalPath() 请问您是否要删除该文件? y/n: );String in scanner.next();if (in.toLowerCase().equals(y)) {file.delete();System.out.println(删除成功!);}}}static void scanDir(File rootDir, String word, ListFile result) {File[] files rootDir.listFiles();if (files null || files.length 0) {return;}for (File file : files) {if (file.isDirectory()) {scanDir(file, word, result);} else {if (file.getName().contains(word)) {result.add(file);}}}}
}2. 案例2
进行普通文件的复制
import java.io.*;
import java.util.Scanner;/*
* 进行普通文件的复制
* */
public class Demo11 {public static void main(String[] args) throws IOException {Scanner scanner new Scanner(System.in);System.out.println(请输入要复制的源文件路径: );String srcPath scanner.nextLine();File srcFile new File(srcPath);if (!srcFile.exists()) {System.out.println(输入文件不存在, 退出程序);return;}if (!srcFile.isFile()) {System.out.println(输入文件不合法, 退出程序);return;}System.out.println(请输入要复制到的目标路径: );String dstPath scanner.nextLine();File dstFile new File(dstPath);if (dstFile.exists()) {if (dstFile.isDirectory()) {System.out.println(该文件已存在, 并且是一个目录文件, 请确定路径是否正确, 退出程序);return;}if (dstFile.isFile()) {System.out.println(该文件已存在, 请问是否进行覆盖? y/n: );String ans scanner.nextLine();if (ans.toLowerCase().equals(n)) {System.out.println(停止复制);return;}}}try (InputStream inputStream new FileInputStream(srcFile);OutputStream outputStream new FileOutputStream(dstFile)) {while (true) {byte[] buf new byte[1024];int len inputStream.read(buf);if (len -1) {break;}outputStream.write(buf, 0, len);}}System.out.println(复制完成);}
}3. 案例3
扫描指定目录并找到名称或者内容中包含指定字符的所有普通文件不包含目录该案例启示就是案例1的升级版。
注意我们现在的方案性能较差所以尽量不要在太复杂的目录下或者大文件下实验
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;/*
* 扫描指定目录
* 并找到名称或者内容中包含指定字符的所有普通文件不包含目录
* */
public class Demo12 {public static void main(String[] args) throws IOException {Scanner scanner new Scanner(System.in);System.out.println(请输入您要搜索的根目录: );String rootPath scanner.nextLine();File rootDir new File(rootPath);if (!rootDir.isDirectory()) {System.out.println(您输入的文件不存在或者不是目录, 退出程序);return;}System.out.println(请输入您要搜索的关键词: );String word scanner.nextLine();ListFile result new ArrayList();scanDir(rootDir, word, result);System.out.println(搜索完成, 共搜索到 result.size() 个文件, 分别是);for (File file : result) {System.out.println(file.getCanonicalPath() 请问您是否要删除该文件? y/n: );String in scanner.next();if (in.toLowerCase().equals(y)) {file.delete();System.out.println(删除成功!);}}}static void scanDir(File rootDir, String word, ListFile result) throws IOException {File[] files rootDir.listFiles();if (files null || files.length 0) {return;}for (File file : files) {if (file.isDirectory()) {scanDir(file, word, result);} else {if (isContentContanis(file, word)) {result.add(file);}}}}static boolean isContentContanis(File file, String word) throws IOException {StringBuffer stringBuffer new StringBuffer();try (InputStream inputStream new FileInputStream(file);Scanner scanner new Scanner(inputStream)) {while (scanner.hasNextLine()) {stringBuffer.append(scanner.nextLine());stringBuffer.append(\r\n);}return stringBuffer.indexOf(word) ! -1;}}
}总结
✨ 想了解更多知识, 请持续关注博主, 本人会不断更新学习记录, 跟随我一起不断学习. ✨ 感谢你们的耐心阅读, 博主本人也是一名学生, 也还有需要很多学习的东西. 写这篇文章是以本人所学内容为基础, 日后也会不断更新自己的学习记录, 我们一起努力进步, 变得优秀, 小小菜鸟, 也能有大大梦想, 关注我, 一起学习.
再次感谢你们的阅读, 你们的鼓励是我创作的最大动力!!!!!