JAVA中如何用线程读取一个文件里的数据!

JAVA中如何用线程读取一个文件里的数据!要求详细步骤!!!谢谢

import java.io.*;
class DownThread extends Thread {
//定义字节数组(取水的竹筒)的长度
private final int BUFF_LEN = 32;
//定义读取的起始点
private long start;
//定义读取的结束点
private long end;
//读取文件对应的输入流
private InputStream is;
//将读取到的字节输出到raf中
private RandomAccessFile raf;

//构造器,传入输入流,输出流和读取起始点、结束点
public DownThread(long start, long end, InputStream is, RandomAccessFile raf) {
//输出该线程负责读取的字节位置
System.out.println(start + "---->" + end);
this.start = start;
this.end = end;
this.is = is;
this.raf = raf;
}

public void run() {
try {
is.skip(start);
raf.seek(start);
//定义读取输入流内容的的缓存数组(竹筒)
byte[] buff = new byte[BUFF_LEN];
//本线程负责读取文件的大小
long contentLen = end - start;
//定义最多需要读取几次就可以完成本线程的读取
long times = contentLen / BUFF_LEN + 4;
//实际读取的字节数
int hasRead = 0;
for (int i = 0; i < times; i++) {
hasRead = is.read(buff);
//如果读取的字节数小于0,则退出循环!
if (hasRead < 0) {
break;
}
raf.write(buff, 0, hasRead);
}
} catch (Exception ex) {
ex.printStackTrace();
}
//使用finally块来关闭当前线程的输入流、输出流
finally {
try {
if (is != null) {
is.close();
}
if (raf != null) {
raf.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}

public class MutilDown {
public static void main(String[] args) {
final int DOWN_THREAD_NUM = 4;
final String OUT_FILE_NAME = "d:/copy勇敢的心.rmvb";
InputStream[] isArr = new InputStream[DOWN_THREAD_NUM];
RandomAccessFile[] outArr = new RandomAccessFile[DOWN_THREAD_NUM];
try {

isArr[0] = new FileInputStream("d:/勇敢的心.rmvb");
long fileLen = getFileLength(new File("d:/勇敢的心.rmvb"));
System.out.println("文件的大小" + fileLen);
//以输出文件名创建第一个RandomAccessFile输出流
outArr[0] = new RandomAccessFile(OUT_FILE_NAME, "rw");
//创建一个与文件相同大小的空文件
for (int i = 0; i < fileLen; i++) {
outArr[0].write(0);
}
//每线程应该读取的字节数
long numPerThred = fileLen / DOWN_THREAD_NUM;
//整个文件整除后剩下的余数
long left = fileLen % DOWN_THREAD_NUM;
for (int i = 0; i < DOWN_THREAD_NUM; i++) {
//为每个线程打开一个输入流、一个RandomAccessFile对象,
//让每个线程分别负责读取文件的不同部分。
if (i != 0) {

isArr[i] = new FileInputStream("d:/勇敢的心.rmvb");
//以指定输出文件创建多个RandomAccessFile对象
outArr[i] = new RandomAccessFile(OUT_FILE_NAME, "rw");
}
if (i == DOWN_THREAD_NUM - 1) {
//最后一个线程读取指定numPerThred+left个字节
new DownThread(i * numPerThred, (i + 1) * numPerThred
+ left, isArr[i], outArr[i]).start();
} else {
//每个线程负责读取一定的numPerThred个字节
new DownThread(i * numPerThred, (i + 1) * numPerThred,
isArr[i], outArr[i]).start();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

public static long getFileLength(File file) {
long length = 0;
//获取文件的长度
long size = file.length();
length = size;
return length;
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-08-31
________________第一个类______读取内容__写入内容____________________
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)+"毫秒");
}
}本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网