当前位置: 首页 > news >正文

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类 二.扫描指定目录&#xff0c;并找到名称中包含指定字符的所有普通文件&#xff08;不包含目录&#xff09;&#xff0c;并且后续询问用户是否要删除该文件 我的代码: import java.io.File; import java.io.IOException; import java.util.Scanner;public class Tes…...

pyqt5 如何终止正在执行的线程?

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

力扣第357场周赛补题

6925. 故障键盘 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff1a;模拟 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年&#xff0c;是一家深耕于大物流领域的人工智能公司&#xff0c;迄今为止已为全球16个国家和地区&#xff0c;120余家客户打造智能化升级体验&#xff0c;场景覆盖海陆空铁、工厂等货运物流领域。 该公司使用开源LDAP面临的挑战 挑战1 开源…...

解决log4j.xml的url没有注册问题

在对log4j.xml配置文件配置时出现http//jakarta.apache.org/log4j/爆红&#xff0c;IDEA提示uri is not registered。源代码如下 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j"http://jakarta.apache.org/lo…...

深度思考操作系统面经

1 堆和栈的区别&#xff1a;&#xff08;如果记的不太清楚&#xff0c;可以类比jvm中的堆和栈的区别&#xff0c;大差不差&#xff09; 存储位置&#xff1a;堆是在计算机内存中动态分配的区域&#xff0c;而栈是在计算机内存中由操作系统自动分配和管理的区域。管理方式&…...

智慧工地源码:数字孪生智慧工地可视化解决方案

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

解决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相比有哪些优点吧&#xff01; 首先&#xff0c;我们要知道&#xff0c;SQLAlchemy是一个Python的SQL工具包和对象关系映射&#xff08;ORM&#xff09;系统&#xff0c;它把Python的面向对象编程&#xff08;OOP&#xff09;的理念带入了数…...

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 写了一个加载动画效果&#xff0c;使用了一个包含多个 <span> 元素的 <div> 元素&#xff0c;并为每个 <span> 元素设置了一个自定义属性 --i。 这段代码创建了一个简单的动态加载动画&#xff0c;由20个垂直排列的…...

使用正则表达式替换文本中的html标签

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

当向数据库导入大量数据时,mysql主键唯一键重复插入,如何丝滑操作并不导入重复数据呢

解决办法&#xff1a; 答案来源&#xff1a;...

【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】文件夹&#xff0c;新建data.js文件&#xff0c;并定义好json数据格式。如下&#xff1a; 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 方式二&#xff08;推荐&#xff09; 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.完成安装向导&#xff0c;登录管理界面即可…...

springboot 百货中心供应链管理系统小程序

一、前言 随着我国经济迅速发展&#xff0c;人们对手机的需求越来越大&#xff0c;各种手机软件也都在被广泛应用&#xff0c;但是对于手机进行数据信息管理&#xff0c;对于手机的各种软件也是备受用户的喜爱&#xff0c;百货中心供应链管理系统被用户普遍使用&#xff0c;为方…...

Golang dig框架与GraphQL的完美结合

将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用&#xff0c;可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器&#xff0c;能够帮助开发者更好地管理复杂的依赖关系&#xff0c;而 GraphQL 则是一种用于 API 的查询语言&#xff0c;能够提…...

Python实现prophet 理论及参数优化

文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候&#xff0c;写过一篇简单实现&#xff0c;后期随着对该模型的深入研究&#xff0c;本次记录涉及到prophet 的公式以及参数调优&#xff0c;从公式可以更直观…...

(二)原型模式

原型的功能是将一个已经存在的对象作为源目标,其余对象都是通过这个源目标创建。发挥复制的作用就是原型模式的核心思想。 一、源型模式的定义 原型模式是指第二次创建对象可以通过复制已经存在的原型对象来实现,忽略对象创建过程中的其它细节。 📌 核心特点: 避免重复初…...

网络编程(UDP编程)

思维导图 UDP基础编程&#xff08;单播&#xff09; 1.流程图 服务器&#xff1a;短信的接收方 创建套接字 (socket)-----------------------------------------》有手机指定网络信息-----------------------------------------------》有号码绑定套接字 (bind)--------------…...

Mysql8 忘记密码重置,以及问题解决

1.使用免密登录 找到配置MySQL文件&#xff0c;我的文件路径是/etc/mysql/my.cnf&#xff0c;有的人的是/etc/mysql/mysql.cnf 在里最后加入 skip-grant-tables重启MySQL服务 service mysql restartShutting down MySQL… SUCCESS! Starting MySQL… SUCCESS! 重启成功 2.登…...

Go语言多线程问题

打印零与奇偶数&#xff08;leetcode 1116&#xff09; 方法1&#xff1a;使用互斥锁和条件变量 package mainimport ("fmt""sync" )type ZeroEvenOdd struct {n intzeroMutex sync.MutexevenMutex sync.MutexoddMutex sync.Mutexcurrent int…...

【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信 BLE Mesh协议的拓扑结构 定向转发机制

目录 节点的功能承载层&#xff08;GATT/Adv&#xff09;局限性&#xff1a; 拓扑关系定向转发机制定向转发意义 CG 节点的功能 节点的功能由节点支持的特性和功能决定。所有节点都能够发送和接收网格消息。节点还可以选择支持一个或多个附加功能&#xff0c;如 Configuration …...

Modbus RTU与Modbus TCP详解指南

目录 1. Modbus协议基础 1.1 什么是Modbus? 1.2 Modbus协议历史 1.3 Modbus协议族 1.4 Modbus通信模型 🎭 主从架构 🔄 请求响应模式 2. Modbus RTU详解 2.1 RTU是什么? 2.2 RTU物理层 🔌 连接方式 ⚡ 通信参数 2.3 RTU数据帧格式 📦 帧结构详解 🔍…...

用js实现常见排序算法

以下是几种常见排序算法的 JS实现&#xff0c;包括选择排序、冒泡排序、插入排序、快速排序和归并排序&#xff0c;以及每种算法的特点和复杂度分析 1. 选择排序&#xff08;Selection Sort&#xff09; 核心思想&#xff1a;每次从未排序部分选择最小元素&#xff0c;与未排…...