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

文件操作与IO(一些小项目)

在指定目录中寻找文件

扫描指定目录,并找到名称对应的所有文件(不包含目录).

import java.io.File;
import java.util.Scanner;public class Project1 {public static void main(String[] args) {//1.输入必要的信息Scanner sc = new Scanner(System.in);System.out.println("请输入要搜索的文件名: ");String fileName = sc.next();System.out.println("请输入要搜索的目录: ");String rootPath = sc.next();File rootFile = new File(rootPath);if(!rootFile.isDirectory()) {System.out.println("输入的答案有误!");return;}// 2.有了要搜索的路径之后,就可以按照递归的方式来搜索// 知道递归的起点, 还需要知道查询的文件名.scanDir(rootFile, fileName);}private static void scanDir(File rootFile, String fileName) {//1.把当前目录中的文件和子目录都列出来File[] files = rootFile.listFiles();if(files == null) {//空的目录,返回即可return;}//2.遍历上述files,判定每一个file是目录还是文件for(File f : files) {System.out.println("当前遍历到: " + f.getAbsolutePath());if(f.isFile()) {//普通文件,判定文件名是否是搜索的文件if(fileName.equals(f.getName())) {System.out.println("找到了符合要求的文件! " + f.getAbsolutePath());}} else if (f.isDirectory()) {//目录文件,就需要进一步的递归scanDir(f, fileName);} else {//这个else暂时不需要;}}}
}

进行普通文件的复制

import java.io.*;
import java.util.Scanner;public class Project2 {public static void main(String[] args) throws IOException {Scanner sc = new Scanner(System.in);System.out.println("请输入要复制的源文件: ");String srcPath = sc.next();System.out.println("请输入要复制的目标文件");String destPath = sc.next();//合法性判定// 1)srcPath对应的文件是否存在File srcFile = new File(srcPath);if(!srcFile.isFile()) {System.out.println("源文件路径有误");return;}//2)destPath不要求对应的文件存在,但是目录得存在.File destFile = new File(destPath);if(!destFile.getParentFile().isDirectory()) {System.out.println("目标路径有误!");return;}//复制操作try(InputStream inputStream = new FileInputStream(srcFile);OutputStream outputStream = new FileOutputStream(destFile)) {while(true) {byte[] buffer = new byte[1024];int n = inputStream.read(buffer);if(n == -1) {//读取完毕break;}//把读到的内容写入到outputStream中outputStream.write(buffer, 0, n);}}}
}

 查找包含指定字符的文件

import java.io.*;
import java.util.Scanner;public class Project3 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("请输入要搜索的路径: ");String rootPath = sc.next();System.out.println("请输入要查询的词: ");String word = sc.next();File rootFile = new File(rootPath);if(!rootFile.isDirectory()) {System.out.println("输入的路径不正确");return;}scanDir(rootFile, word);}private static void scanDir(File rootFile, String word) {File[] files = rootFile.listFiles();if(files == null) {return;}for(File f : files) {System.out.println("当前遍历到: " + f.getAbsolutePath());if(f.isFile()) {//在文件内容中搜索searchInFile(f, word);} else if(f.isDirectory()) {//递归遍历scanDir(f, word);} else {//这个else暂时不需要;}}}private static void searchInFile(File f, String word) {//通过这个方法在文件内部进行搜索//1.把文件内容全部读取出来try(InputStream inputStream = new FileInputStream(f)) {StringBuilder stringBuilder = new StringBuilder();while(true) {byte[] buffer = new byte[1024];int n = inputStream.read(buffer);if(n == -1) {break;}//此处只是读取出文件的一部分.需要把文件的整体内容拼接到一起String s = new String(buffer, 0, n);stringBuilder.append(s);}//当文件整体读取完毕,循环结束之后,此时stringBuilder就是包含文件整个内容的字符串了.if(stringBuilder.indexOf(word) == -1) {//没找到要返回return;}//找到了,打印文件路径System.out.println("找到了! " + word + " 存在于 " + f.getAbsolutePath());} catch(IOException e) {throw new RuntimeException();}}
}

 

http://www.lryc.cn/news/288232.html

相关文章:

  • C语言-算法-线性dp
  • Pandas应用-股票分析实战
  • Database history tablesupgraded
  • Dify学习笔记-应用发布(四)
  • 优化用户体验测试应用领域:提升产品质量与用户满意度
  • 顶顶通呼叫中心中间件机器人压力测试配置(mod_cti基于FreeSWITCH)
  • Debezium发布历史87
  • Leetcode131.分割回文串-Palindrome Patitioning-Python-回溯法
  • Java面试——基础篇
  • C++——结构体
  • C++技术要点总结, 面试必备, 收藏起来慢慢看
  • VR数字展厅,平面静态跨越到3D立体化时代
  • Linux中LVM实验
  • 外包干了一个月,技术退步明显。。。。。
  • gitlab.rb主要配置
  • 网络协议基础
  • Mac使用adb调试安卓手机
  • Web 开发 1: Flask 框架介绍和使用
  • Centos7.6之禅道开源版17.6.1安装记录
  • 有趣的代码(简单)
  • Java和Redis实现一个简单的热搜功能
  • 超越传统,想修哪里就修哪里,SUPIR如何通过文本提示实现智能图像修复
  • 《如何画好架构图》学习笔记
  • redis整合
  • 开循环低温样品架节约液氦操作技巧
  • 年薪30W+,待遇翻倍,我的经历值得每个测试人借鉴
  • DEB方式安装elastic search7以及使用
  • [Tomcat] [最全] 目录和文件详解
  • 微信小程序元素/文字在横向和纵向实现居中对齐、两端对齐、左右对齐、上下对齐
  • 兼容树莓派扩展模块,专注工业产品开发的瑞米派强势来袭