当前位置: 首页 > 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;登录管理界面即可…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)

题目&#xff1a;3442. 奇偶频次间的最大差值 I 思路 &#xff1a;哈希&#xff0c;时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况&#xff0c;哈希表这里用数组即可实现。 C版本&#xff1a; class Solution { public:int maxDifference(string s) {int a[26]…...

ElasticSearch搜索引擎之倒排索引及其底层算法

文章目录 一、搜索引擎1、什么是搜索引擎?2、搜索引擎的分类3、常用的搜索引擎4、搜索引擎的特点二、倒排索引1、简介2、为什么倒排索引不用B+树1.创建时间长,文件大。2.其次,树深,IO次数可怕。3.索引可能会失效。4.精准度差。三. 倒排索引四、算法1、Term Index的算法2、 …...

Spring AI 入门:Java 开发者的生成式 AI 实践之路

一、Spring AI 简介 在人工智能技术快速迭代的今天&#xff0c;Spring AI 作为 Spring 生态系统的新生力量&#xff0c;正在成为 Java 开发者拥抱生成式 AI 的最佳选择。该框架通过模块化设计实现了与主流 AI 服务&#xff08;如 OpenAI、Anthropic&#xff09;的无缝对接&…...

Java求职者面试指南:Spring、Spring Boot、MyBatis框架与计算机基础问题解析

Java求职者面试指南&#xff1a;Spring、Spring Boot、MyBatis框架与计算机基础问题解析 一、第一轮提问&#xff08;基础概念问题&#xff09; 1. 请解释Spring框架的核心容器是什么&#xff1f;它在Spring中起到什么作用&#xff1f; Spring框架的核心容器是IoC容器&#…...

GitHub 趋势日报 (2025年06月06日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图 590 cognee 551 onlook 399 project-based-learning 348 build-your-own-x 320 ne…...

day36-多路IO复用

一、基本概念 &#xff08;服务器多客户端模型&#xff09; 定义&#xff1a;单线程或单进程同时监测若干个文件描述符是否可以执行IO操作的能力 作用&#xff1a;应用程序通常需要处理来自多条事件流中的事件&#xff0c;比如我现在用的电脑&#xff0c;需要同时处理键盘鼠标…...

tomcat入门

1 tomcat 是什么 apache开发的web服务器可以为java web程序提供运行环境tomcat是一款高效&#xff0c;稳定&#xff0c;易于使用的web服务器tomcathttp服务器Servlet服务器 2 tomcat 目录介绍 -bin #存放tomcat的脚本 -conf #存放tomcat的配置文件 ---catalina.policy #to…...

Ubuntu Cursor升级成v1.0

0. 当前版本低 使用当前 Cursor v0.50时 GitHub Copilot Chat 打不开&#xff0c;快捷键也不好用&#xff0c;当看到 Cursor 升级后&#xff0c;还是蛮高兴的 1. 下载 Cursor 下载地址&#xff1a;https://www.cursor.com/cn/downloads 点击下载 Linux (x64) &#xff0c;…...

Axure 下拉框联动

实现选省、选完省之后选对应省份下的市区...

Linux系统:进程间通信-匿名与命名管道

本节重点 匿名管道的概念与原理匿名管道的创建命名管道的概念与原理命名管道的创建两者的差异与联系命名管道实现EchoServer 一、管道 管道&#xff08;Pipe&#xff09;是一种进程间通信&#xff08;IPC, Inter-Process Communication&#xff09;机制&#xff0c;用于在不…...