Java 多线程查找文件中的内容
学过了操作系统,突然不知道多线程有什么用了。
看了一下百度,发现多线程,可以提升系统利用率
在系统进行IO操作的时候,CPU可以处理一些其他的东西,等IO读取到内存后,CPU再处理之前的操作。
总之可以在用户层面,可以提升效率,不过,有时候多线程设计不当,调试也很麻烦
今天尝试一下简单的查找文件后缀以txt 里的内容 demo
SeachFileContent类,用来查询文件内容
package thread; import java.io.File;
import java.io.IOException; public class SeachFileContent { public static void main(String[] args) {
File file = new File("D://TT//Draymonder");
if(!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}else {
String content="复制";
searchFileContent(file,content);
}
} public static void searchFileContent(File file, String content) {
if(file.isFile()) {
if(file.getName().toLowerCase().endsWith(".txt")) {
new SearchFileThread(file, content).start();
}
}
if(file.isDirectory()) {
File[] files = file.listFiles();
for(File singlefile : files) {
searchFileContent(singlefile, content);
}
}
}
}
SearchFileThread类,用来多线程处理文件内容的查询
package thread; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException; public class SearchFileThread extends Thread {
private File file;
private String content;
SearchFileThread() {} SearchFileThread(File file, String content) {
this.file = file;
this.content = content;
} private String getFileContent(File file) {
try(FileReader fileReader = new FileReader(file)) {
char []all = new char[(int)file.length()];
fileReader.read(all);
return new String(all);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@Override
public void run() {
String str = getFileContent(this.file);
if(null != str) {
if(str.contains(this.content)) {
System.out.printf("在%s中含有 目标串\"%s\" ...%n",this.file.getName(),this.content);
}
}
}
}
Java 多线程查找文件中的内容的更多相关文章
- linux命令---查找文件中的内容
linux命令---查找文件中的内容 [yang@localhost ~]$ cat 1.txt |egrep '123456789|second'-------匹配123456789或者seco ...
- java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- java代码将excel文件中的内容列表转换成JS文件输出
思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...
- Java IO把一个文件中的内容以字符串的形式读出来
代码记录(备查): /** * 把一个文件中的内容以字符串的形式读出来 * * @author zhipengs * */ public class FileToString { public sta ...
- 在java中读取文件中的内容
package shi; import java.io.*; public class wenjianIO { public static void main(String agrs[]){ File ...
- JAVA多线程读写文件范例
在写之前先声明,本文是基于之前在博客园网站上检索到的一份JAVA多线程读写文件的示例,我在写自己的程序时是在那位作者写的基础上做了改良,但已不记得原文的地址.如果有知情者,烦请帖出地址,我在此文上加入 ...
- 从Excel文件中读取内容
从Excel文件中读取内容 global::System.Web.HttpPostedFileBase file = Request.Files["txtFile"]; strin ...
- Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素-un
ylbtech-Java-Runoob-高级教程-实例-数组:10. Java 实例 – 查找数组中的重复元素 1.返回顶部 1. Java 实例 - 查找数组中的重复元素 Java 实例 以下实例 ...
- 向txt文件中写入内容(覆盖重写与在末尾续写+FileOutputStream与FileWriter)(转发:https://blog.csdn.net/bestcxx/article/details/51381460)
!!!! 读取txt文件中的内容 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; /** ...
随机推荐
- Linux和Windows下查看环境变量方法(转)
add by zhj: 本文中的Linux是指Ubuntu14.04 以前我对环境变量有误解,以为环境变量就是PATH这个变量.其实环境变量其实有很多,PATH仅仅是其中一个而已,比如在Windows ...
- mysql 操作sql语句 操作数据表中的内容/记录
#3. 操作文件中的内容/记录 往哪张表去插入 insert into 表名指定字段(id,name) 插入要加values(针对前面字段插入)(2,mike); insert into t1(id, ...
- OC导航栏自定义返回按钮
[iOS]让我们一次性解决导航栏的所有问题 在默认情况下,导航栏返回按钮长这个样子 导航栏默认返回按钮 导航栏左上角的返回按钮,其文本默认为上一个ViewController的标题,如果上一个Vi ...
- Hadoop 之日志管理—应用在 YARN 中运行时的日志
背景: 在写这篇博文前,自己一直没有弄明白一个问题,“在 Map 函数和 Reduce 函数中使用 System.out.print 打印日志时,输出内容在哪里显示?”.试了好多回,在 log/* 目 ...
- R之ddlpy函数学习[转载]
转自:https://www.cnblogs.com/aloiswei/p/6032513.html 1.函数 ddply(.data, .variables, .fun = NULL, ..., . ...
- [LeetCode] 286. Walls and Gates_Medium tag: BFS
You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstac ...
- 菜刀php过waf
关于PHP 一.waf为啥会拦截菜刀.菜刀在连接时,会向server端POST数据,抓包查看: 会看到他向server端post了 @eval(base64_decode($_POST[z0])); ...
- pycharm + git 的集成使用
1. 下载git 和Pycharm并安装 2. 打开Pycharm, 点击 file-->Default Settins-->Version Control-->Git 然后在 P ...
- testng入门教程16数据驱动(把数据写在xml)
testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...
- iOS UI基础-5.0 QQ框架(Storyboard)
1.拉入TabBarController和4个Navigation 2.TabBarController关联Navigation 3.设置消息,拉入一个Button,设置背影 4.联系人,拉入一个Se ...