java 怎么用10个线程去读取文件夹里100个txt文件中的内容,读完之后同步写到一个文件中去。

如题所述

第1个回答  2018-07-23
这个是我写的三个类,用于多线程操作读取文件内容和写入文件内容,不知道是不是你合你味口。
________________第一个类______读取内容__写入内容____________________
package pro;

import java.io.*;
public class ReadFileToWriteOtherFile {

private File oldFile;
private File newFile;
private BufferedReader br;
private BufferedWriter bw;
private String totalString="";
private Boolean flag=true; //用于标记文件名是否存在 true表示存在

public ReadFileToWriteOtherFile()
{
oldFile=null;
newFile=null;
br=null;
bw=null;
System.out.println("初始化成功");
}
public void readInfoFromFile(String fileName)
{

System.out.println("开始读取");
try
{

oldFile=new File(fileName);
if(oldFile.exists()) //如果文件存在
{
System.out.println("存在");
br=new BufferedReader(new FileReader(oldFile));
String info=br.readLine(); //读取一行
while(info!=null)
{
totalString+=info; //将读取到的一行添加到totalString中
info=br.readLine(); //再读取下一行
//System.out.println(totalString);
}
System.out.println("读取完成,准备写入…………");
}
else //如果文件不存在
{
System.out.println("文件不存在");
flag=false; //标记该文件不存在
}
// System.out.println("totalString="+totalString);
}
catch(FileNotFoundException e)
{
System.out.println(e);System.out.println("开始读取中1");
}
catch(IOException e)
{System.out.println(e);System.out.println("开始读取中2");}

}
public void writeInfoToFile(String fileName)
{
if(!flag) //如果标记前面的文件不存在,则return
{
flag=true; //改回原来的文件标记符
return;
}
try
{
newFile=new File(fileName);
if(newFile.exists()) //如果存在,不用创建新文件
{
System.out.println("文件存在,可以写入!");
}
else //如果不存在,则创建一个新文件
{
System.out.println("文件不存在,准备创建新文件");
newFile.createNewFile();
System.out.println("文件创建成功,可以写入");
}
bw=new BufferedWriter(new FileWriter(newFile,true));
// System.out.println("totalString="+totalString);
bw.write(totalString,0,totalString.length());
bw.flush(); //刷新缓冲区
System.out.println("写入完成");
totalString="\r\t"; //清空原来的字符串
}
catch(FileNotFoundException e)
{System.out.println(e);}
catch(IOException e)
{System.out.println(e);}

}
}
________________第二个类______一个自定义的线程类____________________
package pro;

import java.lang.Thread;
public class MyThread extends Thread
{
private int index; //用于数组的位置
private String[] fileNames; //定义一个字符串数组
ReadFileToWriteOtherFile bftwo=new ReadFileToWriteOtherFile(); //定义前面的自定义类
public MyThread(String[] fileNames,int index) //index表示数组位置标号
{
this.index=index;
this.fileNames=fileNames;
}
public void run()
{

bftwo.readInfoFromFile(fileNames[index]);//传入数组中的字符串参数
bftwo.writeInfoToFile("b.txt"); //传入写入的目的地文件
//index++; //数组位置加1
System.out.println("==============");//分隔线

}
}
________________第三个类______主程序____________________
package pro;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.*;
public class BeanRunApp {

/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args)
{
/* ApplicationContext apc=new ClassPathXmlApplicationContext("beans.xml");
ClassRoom croom=(ClassRoom)apc.getBean("classRoom");
croom.out();
System.out.println("over");
*/
long startTime=System.currentTimeMillis();
String[] a={"a.txt","c.txt","d.txt","e.txt"}; //用一个符品数组保存文件名

for(int i=0;i<a.length;i++) //用数组的长度来作为循环条件
{ //把这个数组和i的值作为构造函数传入线程类
MyThread myth=new MyThread(a,i);
System.out.println("--------------------------------");
myth.start(); //执行
System.out.println("当前的线程是:"+myth.getName());
}
long endTime=System.currentTimeMillis();
System.out.println("耗时:"+(endTime-startTime)+"毫秒");
}
}