黑马程序员技术交流社区

标题: 请教为什么hashmap没有值? [打印本页]

作者: 张小锋    时间: 2012-5-9 06:55
标题: 请教为什么hashmap没有值?
本帖最后由 张小锋 于 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
}
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2