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;
}
}
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)+"毫秒");
}
}本回答被网友采纳
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)+"毫秒");
}
}本回答被网友采纳