java文件

news/2025/2/15 21:39:57/

一.File类

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

我的代码:

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


http://www.ppmy.cn/news/1013412.html

相关文章

SQLAlchemy与标准SQL相比有哪些优点?

让我来给你讲讲SQLAlchemy和标准SQL相比有哪些优点吧&#xff01; 首先&#xff0c;我们要知道&#xff0c;SQLAlchemy是一个Python的SQL工具包和对象关系映射&#xff08;ORM&#xff09;系统&#xff0c;它把Python的面向对象编程&#xff08;OOP&#xff09;的理念带入了数…

机器学习笔记之优化算法(十一)凸函数铺垫:梯度与方向导数

机器学习笔记之优化算法——凸函数铺垫&#xff1a;梯度与方向导数 引言回顾&#xff1a;偏导数方向余弦方向导数方向导数的几何意义方向导数的定义 方向导数与偏导数之间的关联关系证明过程 梯度 ( Gradient ) (\text{Gradient}) (Gradient) 引言 本节作为介绍凸函数的铺垫&a…

FastAPI 构建 API 高性能的 web 框架(一)

如果要部署一些大模型一般langchainfastapi&#xff0c;或者fastchat&#xff0c; 先大概了解一下fastapi,本篇主要就是贴几个实际例子。 官方文档地址&#xff1a; https://fastapi.tiangolo.com/zh/ 1 案例1:复旦MOSS大模型fastapi接口服务 来源&#xff1a;大语言模型工程…

封装上传文件组件(axios,onUploadProgress,取消请求)

目录 定时模拟进度条 方法 A.axios B.xhr 取消请求​​​​​​​ 完整代码 A.自定义上传组件 B.二次封装组件 情况 增加cancelToken不生效&#xff0c;刷新页面 进度条太快->设置浏览器网速 定时模拟进度条 startUpload() {if (!this.file) return;const totalS…

0基础学习VR全景平台篇 第79篇:全景相机-泰科易如何直播推流

泰科易科技是中国的一家研发全景相机的高科技公司&#xff0c;前不久&#xff0c;在2020世界VR产业大会上发布了新一代5G VR直播影像采集终端--360starlight。以其出色的夜景成像效果和一“部”到位的直播方案重新定义了VR慢直播相机&#xff0c;对行业具有高度借鉴意义。 本文…

SpringCloud项目打包注意事项以及可能出错的几种情况

SpringCloud项目打包注意事项和可能出错的几种情况 1、检查子模块中的 parent的pom文件路径 \<relativePath/\>2、检查打包插件的位置3、检查module是否重复引用 欢迎访问我的个人博客&#xff1a;https://wk-blog.vip 1、检查子模块中的 parent的pom文件路径 <relat…

【果树农药喷洒机器人】Part2:机器人变量喷药系统硬件选型

本专栏介绍&#xff1a;付费专栏&#xff0c;持续更新机器人实战项目&#xff0c;欢迎各位订阅关注。 关注我&#xff0c;带你了解更多关于机器人、嵌入式、人工智能等方面的优质文章&#xff01; 文章目录 一、引言二、变量喷药系统总体要求2.1系统功能要求2.2系统技术要求 三…

Java课题笔记~ AOP编程术语(掌握)

&#xff08;1&#xff09; 切面&#xff08;Aspect&#xff09; 切面泛指交叉业务逻辑。上例中的事务处理、日志处理就可以理解为切面。常用的切面是通知&#xff08;Advice&#xff09;。实际就是对主业务逻辑的一种增强。 &#xff08;2&#xff09; 连接点&#xff08;Jo…