java实现超大文件的读写
来源:互联网 发布:杭州大数据培训 编辑:程序博客网 时间:2024/05/05 03:32
一、对于几百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(); } } }
0 0
- java实现超大文件的读写
- java超大文件读写
- java读写文件,读超大文件
- java读写文件,读超大文件
- 读写超大文件方式
- 超大数的java具体实现
- Java实现超大数字相加的算法
- java 超大文件上传
- java读取超大文件
- java读取超大文件
- 文件读写的实用工具(java实现)
- 超大文件的copy
- NSFileHandle简析以及实现超大文件的复制
- POI实现超大数据的Excel的读写操作,支持Excel最大行数。
- java中不用BigInteger实现超大整数的乘法操作
- java nio实现读写文件
- java实现properties文件读写
- Java文件的读写
- 钢架品牌
- STM32中GPIO的8种工作模式!
- 第十三周项目五之字符串操作
- 设计模式--策略模式,装饰模式
- 第十五周项目1-打入“内部”寻“内幕”
- java实现超大文件的读写
- ASP.NET中防止页面多次加载的IsPostBack属性
- 深入理解oracle的事务隔离性
- “电子游戏之父”去世享年92岁 发明首台游戏机
- Codeforces Round #244 (Div. 2)D(字符串DP)
- iOS多语言备选的规则
- 链表的基本操作,创建、删除、插入、反转
- 遇到的问题-----The project contains another dependency with the same archive name
- OGG概述