java實現(xiàn)超大文件的讀寫功能
對于幾百M或上G的大文件可使用java nio進行讀寫 , 根據(jù)個人的需求 可能需要將一個超大文件讀寫形成很多較小的文件進行分析,這也不是什么難事,在讀完一個緩沖區(qū)后 更換寫入的對象即可,本文就不做詳細介紹了,有需要的可以聯(lián)系本人。
直接上程序吧
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(); } } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Java封裝數(shù)組實現(xiàn)在數(shù)組中查詢元素和修改元素操作示例
這篇文章主要介紹了Java封裝數(shù)組實現(xiàn)在數(shù)組中查詢元素和修改元素操作,結合實例形式分析了java針對數(shù)組元素查詢、修改的封裝操作實現(xiàn)技巧,需要的朋友可以參考下2020-03-03Mybatis如何通過接口實現(xiàn)sql執(zhí)行原理解析
為了簡化MyBatis的使用,MyBatis提供了接口方式自動化生成調用過程,可以大大簡化MyBatis的開發(fā),下面這篇文章主要給大家介紹了關于Mybatis如何通過接口實現(xiàn)sql執(zhí)行原理解析的相關資料,需要的朋友可以參考下2023-01-01詳解ConcurrentHashMap如何保證線程安全及底層實現(xiàn)原理
這篇文章主要為大家介紹了ConcurrentHashMap如何保證線程安全及底層實現(xiàn)原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05Spring Boot 集成Mybatis實現(xiàn)主從(多數(shù)據(jù)源)分離方案示例
本篇文章主要介紹了Spring Boot 集成Mybatis實現(xiàn)主從(多數(shù)據(jù)源)分離方案實例,具有一定的參考價值,有興趣的可以了解一下。2017-03-03SpringBoot整合Lucene實現(xiàn)全文檢索的詳細步驟
全文搜索(Full-Text?Search)是指對大規(guī)模存儲在計算機系統(tǒng)中的文本數(shù)據(jù)進行檢索和匹配的技術,它允許用戶輸入關鍵字,然后從海量的文本數(shù)據(jù)中快速找到相關的信息,本文介紹了SpringBoot整合Lucene實現(xiàn)全文檢索的詳細步驟,需要的朋友可以參考下2024-03-03