java文件
一.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;}
}
相关文章:

java文件
一.File类 二.扫描指定目录,并找到名称中包含指定字符的所有普通文件(不包含目录),并且后续询问用户是否要删除该文件 我的代码: import java.io.File; import java.io.IOException; import java.util.Scanner;public class Tes…...

pyqt5 如何终止正在执行的线程?
在 PyQt5 中终止正在执行的线程,可以通过一些协调的方法来实现。一般情况下,直接强行终止线程是不安全的,可能会导致资源泄漏或者程序异常。相反,我们可以使用一种协作的方式,通知线程在合适的时候自行退出。 以下是一…...

力扣第357场周赛补题
6925. 故障键盘 - 力扣(LeetCode) 思路:模拟 class Solution { public:string finalString(string s) {string res;for(auto c : s){if(c i) reverse(res.begin(), res.end());else res c;}return res;} }; 6953. 判断是否能拆分数组 - 力…...

Keras指定model.fit()的输出
model.fit()当verbose1的时候会打印出所有指标和loss, 在多输出的情况下更是一团乱麻. 下面是一个可以指定每个epoch训练完的输入指标的方法: from keras.callbacks import Callback# Custom callback to display loss only at the end of each epoch class LossCallback(Call…...

替换开源LDAP,某科技企业用宁盾目录统一身份,为业务敏捷提供支撑
客户介绍 某高科技企业成立于2015年,是一家深耕于大物流领域的人工智能公司,迄今为止已为全球16个国家和地区,120余家客户打造智能化升级体验,场景覆盖海陆空铁、工厂等货运物流领域。 该公司使用开源LDAP面临的挑战 挑战1 开源…...

解决log4j.xml的url没有注册问题
在对log4j.xml配置文件配置时出现http//jakarta.apache.org/log4j/爆红,IDEA提示uri is not registered。源代码如下 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j"http://jakarta.apache.org/lo…...

深度思考操作系统面经
1 堆和栈的区别:(如果记的不太清楚,可以类比jvm中的堆和栈的区别,大差不差) 存储位置:堆是在计算机内存中动态分配的区域,而栈是在计算机内存中由操作系统自动分配和管理的区域。管理方式&…...

智慧工地源码:数字孪生智慧工地可视化解决方案
一、智慧工地建设背景 我国经济发展正从传统粗放式的高速增长阶段,进入高效率、低成本、可持续的中高速增长阶段。随着现代建筑的复杂度和体量等不断增加,施工现场管理的内容越来越多,管理的技术难度和要求在不断提高。传统的施工现场管理模…...

解决rockchip平台Android13系统以太网设置静态IP保存不了问题
前言 rk平台平Android13系统测试以太网,发现设置静态IP保存不了问题,即设置静态IP以后重启系统,IP又变成动态的了。 分析 抓取log发现保存静态IP的时候会打印如下log: 08-07 06:22:28.377 626 749 D EthernetNetworkFactory: updateInterface, iface: eth0, ipConfi…...

SQLAlchemy与标准SQL相比有哪些优点?
让我来给你讲讲SQLAlchemy和标准SQL相比有哪些优点吧! 首先,我们要知道,SQLAlchemy是一个Python的SQL工具包和对象关系映射(ORM)系统,它把Python的面向对象编程(OOP)的理念带入了数…...

Zookeeper与Kafka
Zookeeper与Kafka 一、Zookeeper 概述1.Zookeeper 定义2.Zookeeper 工作机制3.Zookeeper 特点4.Zookeeper 数据结构5.Zookeeper 应用场景6.Zookeeper 选举机制 二、部署 Zookeeper 集群1.准备 3 台服务器做 Zookeeper 集群2.安装 Zookeeper3.拷贝配置好的 Zookeeper 配置文件到…...

MySQL—— 基础语法大全
MySQL—— 基础 一、MySQL概述1.1 、数据库相关概念1.2 、MySQL 客户端连接1.3 、数据模型 二、SQL2.1、SQL通用语法2.2、SQL分类2.3、DDL2.4、DML2.5、DQL2.6、DCL 三、函数四、约束五、多表查询六、事务 一、MySQL概述 1.1 、数据库相关概念 数据库、数据库管理系统、SQL&a…...

css小练习:案例6.炫彩加载
一.效果浏览图 二.实现思路 html部分 HTML 写了一个加载动画效果,使用了一个包含多个 <span> 元素的 <div> 元素,并为每个 <span> 元素设置了一个自定义属性 --i。 这段代码创建了一个简单的动态加载动画,由20个垂直排列的…...

使用正则表达式替换文本中的html标签
文章目录 使用正则表达式替换文本中的html标签原文本:使用正则表达式进行替换替换后:展示 html 文本 使用正则表达式替换文本中的html标签 我们存储 markdown 文章时,如果存储转换后的 html 页面,那么在查出来的时候,…...

当向数据库导入大量数据时,mysql主键唯一键重复插入,如何丝滑操作并不导入重复数据呢
解决办法: 答案来源:...

【go-zero】docker镜像直接部署go-zero的API与RPC服务 如何实现注册发现?docker network 实现 go-zero 注册发现
一、场景&问题 使用docker直接部署go-zero微服务会发现API无法找到RPC服务 1、API无法发现RPC服务 用docker直接部署 我们会发现API无法注册发现RPC服务 原因是我们缺少了docker的network网桥 2、系统内查看 RPC服务运行正常API服务启动,通过docker logs 查看日志还是未…...

微信小程序读取本地json
首先在项目录下新建【server】文件夹,新建data.js文件,并定义好json数据格式。如下: pages/index/index.ts导入data.js并请求json pages/index/index.wxml页面展示数据...

Stephen Wolfram:ChatGPT 的训练
The Training of ChatGPT ChatGPT 的训练 OK, so we’ve now given an outline of how ChatGPT works once it’s set up. But how did it get set up? How were all those 175 billion weights in its neural net determined? Basically they’re the result of very large…...

SpringCloud实用篇2——Nacos配置管理 Feign远程调用 Gateway服务网关
目录 1 Nacos配置管理1.1 统一配置管理1.1.1 在nacos中添加配置文件1.1.2 从微服务拉取配置 1.2 配置热更新1.2.1 方式一1.2.2 方式二(推荐) 1.3.配置共享 2 搭建Nacos集群2.1 集群结构图2.2 搭建集群2.2.1 初始化数据库2.2.2 下载nacos2.2.3 配置Nacos2…...

tomcat配置文件和web站点部署(zrlog)简介
一.tomcat/apache-tomcat-8.5.70/conf/server.xml组件类别介绍 1.类别 2.Connector参数 3.host参数 4.Context参数 二.web站点部署(以zrlog为例) 1.将zrlog的war包传到webapps下面 2.在mysql数据库中创建zrlog用户并赋予权限 3.完成安装向导,登录管理界面即可…...

elementui实现当前页全选+所有全选+翻页保持选中状
原文来自:https://blog.csdn.net/sumimg/article/details/121693305?spm1001.2101.3001.6650.1&utm_mediumdistribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-121693305-blog-127570059.235%5Ev38%5Epc_relevant_anti_t3&depth_1-utm…...

Opencv项目实战:24 石头剪刀布
目录 0、项目介绍 1、效果展示 2、项目搭建 3、项目代码展示与部分讲解 pyzjr库...

Qt--QPlugin插件
写在前面 Qt–动态链接库一文中提到,动态方式加载dll只能加载 extern "C“ 的导出函数,而无法加载类,因此可以使用Qt提供的插件来实现导出类的动态加载。 QPlugin是Qt插件框架的一部分,是一种轻量级的插件系统,…...

公会发展计划 (GAP) 第 4 季:塑造 YGG 的成就版图
基于前三个赛季所取得的成果,Yield Guild Games(YGG)自豪地宣布推出 公会发展计划(GAP)第 4 季。公会最近的一些精英成员将在本季加入公会,公会成员将在全新的任务中磨练自己的技能,建立自己在 …...

ExpressJS教程_编程入门自学教程_菜鸟教程-免费教程分享
教程简介 Express是基于Node.js平台,快速、开放、极简的Web开发框架;通俗的理解:Express的作用和Node.js内置的http模块类似,是专门用来创建Web服务器的;Express的本质:就是一个npm上的第三方包,提供了快速创建Web服务器的便捷方法。ExpressJS是一个Web…...

时序预测 | MATLAB实现BO-BiLSTM贝叶斯优化双向长短期记忆神经网络时间序列预测
时序预测 | MATLAB实现BO-BiLSTM贝叶斯优化双向长短期记忆神经网络时间序列预测 目录 时序预测 | MATLAB实现BO-BiLSTM贝叶斯优化双向长短期记忆神经网络时间序列预测效果一览基本介绍模型搭建程序设计参考资料 效果一览 基本介绍 MATLAB实现BO-BiLSTM贝叶斯优化双向长短期记忆…...

HIVE优化之不需要参数优化
#1.数据倾斜 什么是数据倾斜? 一部分数据多 一部分数据少 造成的结果: MR运行过慢 主要是shuffle和reduce过程慢 分组聚合导致数据倾斜 Hive未优化的分组聚合 方法1:在MAP端直接聚合(分组聚合优化),减少…...

前端 select 标签如何创建下拉菜单?
聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 代码示例⭐ 代码讲解⭐ 写在最后 ⭐ 专栏简介 前端入门之旅:探索Web开发的奇妙世界 记得点击上方或者右侧链接订阅本专栏哦 几何带你启航前端之旅 欢迎来到前端入门之旅!这个专栏是为那些对Web开发感兴趣、刚刚踏…...

基于 eclipse-temurin 构建国内时区,地区,语言的docker镜像
基于 eclipse-temurin 构建国内时区,地区,语言的镜像 使用场景自定Dockerfile构建自己的基础镜像构建本地镜像推送远程仓库 使用场景 在给应用构建自定义镜像时,往往需要在每次构建时去调整时区,地区这些东西;每次构建…...

RunnerGo配置场景时接口模式该怎么选
在进行性能测试时,测试场景的正确配置非常关键。首先,需要根据业务场景和需求,设计出合理的测试场景,再利用相应的工具进行配置,实现自动化的性能测试。 在JMeter中,用户需要自己组织测试场景,…...