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();
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC 接收前端傳遞的參數(shù)四種方式小結(jié)
這篇文章主要介紹了SpringMVC 接收前端傳遞的參數(shù)四種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
Java二分法查找_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了Java二分法查找的相關(guān)資料,需要的朋友可以參考下2017-04-04
使用TraceId在Spring Cloud中實現(xiàn)線上問題快速定位
在微服務(wù)架構(gòu)中,服務(wù)間的互相調(diào)用使得問題定位變得復(fù)雜,在此背景下,TraceId為我們提供了一個在復(fù)雜環(huán)境中追蹤請求路徑和定位問題的工具,本文不僅介紹TraceId的基本概念,還將結(jié)合真實場景,為您展示如何在Spring Cloud中應(yīng)用它2023-09-09
Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的實現(xiàn)方法和原理詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)之紅黑樹的實現(xiàn)方法和原理,紅黑樹是一種特殊的二叉查找樹,每個結(jié)點都要儲存位表示結(jié)點的顏色,或紅或黑,本文將通過示例為大家詳細講講紅黑樹的原理及實現(xiàn),感興趣的朋友可以了解一下2024-02-02
Java list與set中contains()方法效率案例詳解
這篇文章主要介紹了Java list與set中contains()方法效率案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08

