java实现超大文件的读写功能

所属分类: 软件编程 / java 阅读数: 51
收藏 0 赞 0 分享

对于几百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(); 
 } 
} 
 
 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

spring boot 静态资源处理方法

本篇文章主要介绍了spring boot 静态资源处理方法。小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解spring boot 使用application.properties 进行外部配置

这篇文章主要介绍了详解spring boot 使用application.properties 进行外部配置,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java实现分页的前台页面和后台代码

这篇文章主要为大家详细介绍了Java实现分页的前台页面和后台代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解spring面向切面aop拦截器

spring中有很多概念和名词,比如过滤器、拦截器、aop等。这篇文章主要介绍了详解spring面向切面aop拦截器,有兴趣的可以了解一下。
收藏 0 赞 0 分享

Java easyui树形表格TreeGrid的实现代码

这篇文章主要为大家详细介绍了Java easyui树形表格TreeGrid的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java的NIO与IO的详解及对比

这篇文章主要介绍了Java的NIO与IO的详解及对比的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java中网络IO的实现方式(BIO、NIO、AIO)介绍

这篇文章主要介绍了Java中网络IO的实现方式(BIO、NIO、AIO)介绍的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Java 敏感信息加密处理

本文主要介绍了Java 敏感信息加密处理的相关知识:1)敏感信息加密处理我们要实现什么;2)敏感信息加密处理我做了些什么;3)敏感信息加密实现方法。具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详解使用Spring3 实现用户登录以及权限认证

这篇文章主要介绍了详解使用Spring3 实现用户登录以及权限认证,这里整理了详细的代码,有需要的小伙伴可以参考下。
收藏 0 赞 0 分享

Java的内存机制详解

本文主要介绍了Java的内存机制的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧
收藏 0 赞 0 分享
查看更多