java实现超大文件的读写功能
对于几百M或上G的大文件可使用javanio进行读写,根据个人的需求可能需要将一个超大文件读写形成很多较小的文件进行分析,这也不是什么难事,在读完一个缓冲区后更换写入的对象即可,本文就不做详细介绍了,有需要的可以联系本人。
直接上程序吧
packagecn.gzu.readfile; importjava.io.File; importjava.io.IOException; importjava.io.RandomAccessFile; importjava.nio.ByteBuffer; importjava.nio.channels.FileChannel; publicclassReadWriteNio{ publicstaticvoidmain(Stringargs[])throwsException{ intbufSize=100; Filefin=newFile("E:\\jiahui\\2014-09-01.dat"); Filefout=newFile("E:\\jiahui\\res.txt"); System.out.print("开始读取并重写文件,请等待..."); FileChannelfcin=newRandomAccessFile(fin,"r").getChannel(); ByteBufferrBuffer=ByteBuffer.allocate(bufSize); FileChannelfcout=newRandomAccessFile(fout,"rws").getChannel(); ByteBufferwBuffer=ByteBuffer.allocateDirect(bufSize); readFileByLine(bufSize,fcin,rBuffer,fcout,wBuffer); System.out.print("读写完成!"); } /*读文件同时写文件*/ publicstaticvoidreadFileByLine(intbufSize,FileChannelfcin,ByteBufferrBuffer, FileChannelfcout,ByteBufferwBuffer){ StringenterStr="\n"; try{ byte[]bs=newbyte[bufSize]; intsize=0; StringBufferstrBuf=newStringBuffer(""); while((size=fcin.read(rBuffer))!=-1){ //while(fcin.read(rBuffer)!=-1){ if(size>1*1024){ break; } intrSize=rBuffer.position(); rBuffer.rewind(); rBuffer.get(bs); rBuffer.clear(); StringtempString=newString(bs,0,rSize,"UTF-8"); //System.out.println(size+":"+tempString); intfromIndex=0; intendIndex=0; while((endIndex=tempString.indexOf(enterStr,fromIndex))!=-1){ Stringline=tempString.substring(fromIndex,endIndex); line=newString(strBuf.toString()+line+"\n"); System.out.println(size+":"+line); //System.out.print(""); //writetoanthonefile 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(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } /*写文件*/ publicstaticvoidwriteFileByLine(FileChannelfcout,ByteBufferwBuffer,Stringline){ try{ //writeonfilehead //fcout.write(wBuffer.wrap(line.getBytes())); //wirteappendfileonfoot fcout.write(wBuffer.wrap(line.getBytes()),fcout.size()); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。