本帖最后由 张小锋 于 2012-5-9 06:58 编辑
import java.io.*;
import java.util.*;
public class threadRead extends Thread{
File file;
int threadNumber;
int threadCount;
static HashMap<Integer,String> map;
public threadRead(File file,int threadCount,int threadNumber) {代表文件来源,线程个数,线程顺序
this.file=file;
this.threadCount=threadCount;
this.threadNumber=threadNumber;
map=new HashMap<Integer, String>();
}
@Override
public void run() {
int len=(int) file.length();
try {
String str=read(file,(int)len/threadCount*threadNumber,(int)len/threadCount)+"";
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean read(File file,int pointIndex,int readLength) throws Exception{
RandomAccessFile raf=new RandomAccessFile(file,"rw");
byte[] byt=new byte[readLength];
raf.seek(pointIndex);
int ik=raf.read(byt);
System.out.println(this.getName()+"---"+new String(byt)); //此处输出时有值,但集合中没有添加进去
map.put(Integer.valueOf(this.getName()), new String(byt));
return raf.getFilePointer()>file.length();
}
public static void main(String[] args) throws Exception {
int threadCount=4;//线程个数
File file=new File("C:\\a.txt");//文件来源
File toFile=new File("C:\\b.txt");//目标文件
for (int i = 0; i < threadCount; i++) {
Thread thread=new threadRead(file,threadCount,i);
thread.setName(i+"");
thread.start();
}
System.out.println(map.size()); //结果打印0
}
}
|
|