+ -
当前位置:首页 → 问答吧 → nio读取文件转换卡死

nio读取文件转换卡死

时间:2011-08-23

来源:互联网

读取log4j 文件...将文件内容转换为string.通过json传递给前台.我读取一个1.84mb的文件..用了4分钟才返给页面显示。而且myeclipse也光荣的下岗了...用StringBuffer 字符连接...

作者: Andy_wolf_shaw   发布时间: 2011-08-23

读取的时候,循环体编码时有没有考虑性能啊,
循环体中是否包含不需要的重复逻辑呢?这些都会很影响性能的
比如说:转换为String,你是否在循环里面New的啊?
如果是的话,每次循环都New,影响性能哦

作者: laughing__   发布时间: 2011-08-23

我表示无能为力了、、、这个 真不懂 ,帮你顶下

作者: God_Girl   发布时间: 2011-08-23

贴下代码

作者: liuyuhua0066   发布时间: 2011-08-23

读取文件的代码

作者: liuyuhua0066   发布时间: 2011-08-23

FileInputSteam inputStream = new FileInputStream(new File(src));
FileChannel fileChanel = inputStream.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024*3);
byte[] barray = new byte[1024];
StringBuffer result = new StringBuffer();
int nRead, nGet;
while((nRead = fileChanel.read(buffer)) != -1){
  if(nRead == 0 )continue;
  buffer.position(0);
  buffer.limit(nRead);
  while(buffer.hasRemaining()){
  nGet = Math.min(buffer.remaining(),1024);
  buffer.get(barray,0,nGet);
  result.append(barray);
}
}

作者: Andy_wolf_shaw   发布时间: 2011-08-23

buffer 是哪里出来的?

作者: yljames   发布时间: 2011-08-23

buffer=byteBufer...写错了

作者: Andy_wolf_shaw   发布时间: 2011-08-23

热门下载

更多