A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张小锋 中级黑马   /  2012-5-9 06:55  /  1390 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张小锋 于 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
}
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马