java實(shí)現(xiàn)超大文件的讀寫功能
對(duì)于幾百M(fèi)或上G的大文件可使用java nio進(jìn)行讀寫 , 根據(jù)個(gè)人的需求 可能需要將一個(gè)超大文件讀寫形成很多較小的文件進(jìn)行分析,這也不是什么難事,在讀完一個(gè)緩沖區(qū)后 更換寫入的對(duì)象即可,本文就不做詳細(xì)介紹了,有需要的可以聯(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("開始讀取并重寫文件,請(qǐng)等待..."); 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("讀寫完成!"); } /*讀文件同時(shí)寫文件*/ 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(); } } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

詳細(xì)解讀Druid數(shù)據(jù)庫(kù)連接池的使用

Mybatis如何通過(guò)接口實(shí)現(xiàn)sql執(zhí)行原理解析

feign GET請(qǐng)求不支持對(duì)象傳參的坑及解決

Spring Security 強(qiáng)制退出指定用戶的方法

Java實(shí)現(xiàn)調(diào)用MySQL存儲(chǔ)過(guò)程詳解

詳解ConcurrentHashMap如何保證線程安全及底層實(shí)現(xiàn)原理

一個(gè)簡(jiǎn)單的Java文件讀取的進(jìn)度條

Spring Boot 集成Mybatis實(shí)現(xiàn)主從(多數(shù)據(jù)源)分離方案示例

SpringBoot整合Lucene實(shí)現(xiàn)全文檢索的詳細(xì)步驟