题目:
把 查找文件内容 改为多线程查找文件内容
原练习的思路是遍历所有文件,当遍历到文件是 .java的时候,查找这个文件的内容,查找完毕之后,再遍历下一个文件。
现在通过多线程调整这个思路:
遍历所有文件,当遍历到文件是.java的时候,创建一个线程去查找这个文件的内容,不必等待这个线程结束,继续遍历下一个文件
创建多线程有三种方式:
1)继承Thread类
2)实现Runnable接口
3)匿名类的方式
话不多说,直接上代码:
1)继承Thread类。
//线程类
public class ThreadExt extends Thread {
public File file;
public String content;
public ThreadExt(File file, String content) {
this.file = file;
this.content = content;
}
@Override
public void run() {
FileReader fr = null;
try {
fr = new FileReader(file);
char[] ch = new char[1024];
int len;
while ((len = fr.read(ch)) != -1) {
String str = String.valueOf(ch);
if (str.contains(content)) {
System.out.printf("找到包含 %s 的文件,是 %s%n", content, file.getAbsolutePath());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//实现类
public class Test {
public static void search(File file, String content) {
if (!file.exists()) {
System.out.println("文件不存在!");
return;
}
File[] files = file.listFiles();
if (files == null) {
System.out.println("空文件");
return;
}
for (File eachFile : files) {
if (eachFile.isDirectory()) {
search(eachFile, content);
}
if (eachFile.isFile()) {
if (eachFile.getName().endsWith(".java")) {
ThreadExt te = new ThreadExt(eachFile, content);
te.start();
}
}
}
}
public static void main(String[] args) {
File file = new File("F:\\test1");
String str = "a";
search(file, str);
}
}
2)实现Runnable接口。
//线程类
public class RunnableInter implements Runnable {
public File file;
public String content;
public RunnableInter(File file, String content) {
this.file = file;
this.content = content;
}
@Override
public void run() {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
while (true) {
String str = br.readLine();
if (str == null) {
break;
}
if (str.contains(content)) {
System.out.printf("找到包含 %s 的文件,是 %s%n", content, file.getAbsolutePath());
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//实现类
public class Test {
public static void search(File file, String content) {
if (!file.exists()) {
System.out.println("文件不存在!");
return;
}
File[] files = file.listFiles();
if (files == null) {
System.out.println("空文件");
return;
}
for (File eachFile : files) {
if (eachFile.isDirectory()) {
search(eachFile, content);
}
if (eachFile.isFile()) {
if (eachFile.getName().endsWith(".java")) {
RunnableInter ri = new RunnableInter(eachFile, content);
new Thread(ri).start();
}
}
}
}
public static void main(String[] args) {
File file = new File("F:\\test1");
String str = "a";
search(file, str);
}
}
3)匿名类方式。
public class Test {
public static void search(File file, String content) {
if (!file.exists()) {
System.out.println("文件不存在!");
return;
}
File[] files = file.listFiles();
if (files == null) {
System.out.println("空文件");
return;
}
for (File eachFile : files) {
if (eachFile.isDirectory()) {
search(eachFile, content);
}
if (eachFile.isFile()) {
if (eachFile.getName().endsWith(".java")) {
/*RunnableInter ri = new RunnableInter(eachFile, content);
new Thread(ri).start();*/
/*ThreadExt te = new ThreadExt(eachFile, content);
te.start();*/
Thread t1 = new Thread() {
@Override
public void run() {
FileInputStream fis = null;
try {
fis = new FileInputStream(eachFile);
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
String str = new String(bytes);
if (str.contains(content)) {
System.out.printf("找到包含 %s 的文件,是 %s%n", content, eachFile.getAbsolutePath());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
};
t1.start();
}
}
}
}
public static void main(String[] args) {
File file = new File("F:\\test1");
String str = "a";
search(file, str);
}
}
每一种方式用不同的读取方法,也算是对之前的IO练习一下吧。