当前位置: 移动技术网 > IT编程>开发语言>Java > java实现超大文件的读写功能

java实现超大文件的读写功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

对于几百m或上g的大文件可使用java nio进行读写 , 根据个人的需求 可能需要将一个超大文件读写形成很多较小的文件进行分析,这也不是什么难事,在读完一个缓冲区后 更换写入的对象即可,本文就不做详细介绍了,有需要的可以联系本人。

直接上程序吧

package cn.gzu.readfile;
 import java.io.file; 
 import java.io.ioexception; 
 import java.io.randomaccessfile; 
 import java.nio.bytebuffer; 
 import java.nio.channels.filechannel; 
 
 
public class readwritenio { 
 
public static void main(string args[]) throws exception{ 
 int bufsize = 100; 
 file fin = new file("e:\\jiahui\\2014-09-01.dat"); 
 file fout = new file("e:\\jiahui\\res.txt"); 
 
 system.out.print("开始读取并重写文件,请等待...");
 
 filechannel fcin = new randomaccessfile(fin, "r").getchannel(); 
 bytebuffer rbuffer = bytebuffer.allocate(bufsize); 
 
 filechannel fcout = new randomaccessfile(fout, "rws").getchannel(); 
 bytebuffer wbuffer = bytebuffer.allocatedirect(bufsize); 
 
 readfilebyline(bufsize, fcin, rbuffer, fcout, wbuffer); 
 
 system.out.print("读写完成!"); 
} 
 
 /*读文件同时写文件*/
public static void readfilebyline(int bufsize, filechannel fcin, bytebuffer rbuffer, 
 filechannel fcout, bytebuffer wbuffer){ 
 string enterstr = "\n"; 
 try{ 
 byte[] bs = new byte[bufsize]; 
 
 int size = 0; 
 stringbuffer strbuf = new stringbuffer(""); 
 while((size = fcin.read(rbuffer)) != -1){ 
// while(fcin.read(rbuffer) != -1){ 
 if(size > 1*1024){
 break;
 }
 
 int rsize = rbuffer.position(); 
 rbuffer.rewind(); 
 rbuffer.get(bs); 
 rbuffer.clear(); 
 string tempstring = new string(bs, 0, rsize,"utf-8"); 
 // system.out.println(size+": "+tempstring); 
 
 int fromindex = 0; 
 int endindex = 0; 
 while((endindex = tempstring.indexof(enterstr, fromindex)) != -1){ 
  string line = tempstring.substring(fromindex, endindex); 
  line = new string(strbuf.tostring() + line + "\n"); 
  system.out.println(size+": "+line); 
  //system.out.print("</over/>"); 
  //write to anthone file 
  writefilebyline(fcout, wbuffer, line); 
 
  strbuf.delete(0, strbuf.length()); 
  fromindex = endindex + 1; 
 } 
 if(rsize > tempstring.length()){ 
  strbuf.append(tempstring.substring(fromindex, tempstring.length())); 
 }else{ 
  strbuf.append(tempstring.substring(fromindex, rsize)); 
 } 
 } 
 } catch (ioexception e) { 
 // todo auto-generated catch block 
 e.printstacktrace(); 
 } 
} 
 
 /*写文件*/
public static void writefilebyline(filechannel fcout, bytebuffer wbuffer, string line){ 
 try { 
 //write on file head 
 //fcout.write(wbuffer.wrap(line.getbytes())); 
 //wirte append file on foot 
 fcout.write(wbuffer.wrap(line.getbytes()), fcout.size()); 
 
 } catch (ioexception e) { 
 // todo auto-generated catch block 
 e.printstacktrace(); 
 } 
} 
 
 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网