一直在处理爬虫,经常能遇到读写文件的操作,很多时候都是读写超大文件,记录如下:一、读文件import java.io.BufferedOutputStream;import  java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStreamWriter;public void ReadFile(String filename) {File file=new File(filename);BufferedReader reader=null;try{reader=new BufferedReader(new FileReader(file));   //如果是读大文件  则  new BufferedReader(new FileReader(file),5*1024*1024);  即,设置缓存
String tempString=null;while((tempString=reader.readLine())!=null){//进行操作.....
}reader.close();}catch(IOException e){e.printStackTrace();}finally{if(reader!=null){try{reader.close();}catch(IOException e){e.printStackTrace();}}}}二、写文件import java.io.BufferedOutputStream;import  java.io.BufferedReader;import  java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStreamWriter;public void method1(String file,String content){BufferedWriter out=null;try{out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true)));  //追加的方法
out.write(content);out.write("\r\n");}catch(IOException e){e.printStackTrace();}finally{try{out.close();}catch(IOException e){e.printStackTrace();}}}public void main(String[] args){String filename="D:\a.txt";File f=new File(filename);if(f.exists()){f.delete();}f.createNewFile();String ss="sssssss";method1(filename,ss);}===========================华丽丽的分隔符=============================在实践中发现上面的方法最多也就能处理10M以内的数据,从而编辑此文章。想要真正处理上百M,甚至上G的文件,那就要用到java的nio包:下面的代码转自【http://www.oschina.net/code/snippet_54100_7938】import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; public class TestNio { public static void main(String args[]) throws Exception{
int bufSize = 100;
File fin = new File("D:\\workspace\\test\\usagetracking.log");
File fout = new File("D:\\workspace\\test\\usagetracking2.log"); 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("OK!!!");
} /*读文件同时写文件*/
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(buffer)) != -1){
while(fcin.read(rBuffer) != -1){ int rSize = rBuffer.position(); rBuffer.rewind(); rBuffer.get(bs); rBuffer.clear(); String tempString = new String(bs, 0, rSize); //System.out.print(tempString); //System.out.print("<200>"); 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); //System.out.print(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();
}
} }

转载于:https://www.cnblogs.com/wh-king/p/4324699.html

java读写文件,读超大文件相关推荐

  1. java读写json格式的文件方法详解.txt,并批量存储进redis

    捐躯赴国难,视死忽如归.恸哭六军俱缟素,冲冠一怒为红颜.君子坦荡荡,小人长戚戚.风日晴和人意好,夕阳箫鼓几船归.民为贵,社稷次之,君为轻.Java 读写json格式的文件方法详解 文章录入:7747. ...

  2. java上传大文件_Java超大文件上传解决办法

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  3. Java读写CSV格式的文件

    读取CSV文件 /*** 读取 CSV 文件** @return*/ public static LinkedList<CsvData> readCSV(String path) {Lin ...

  4. Java 读写json格式的文件方法详解

    2019独角兽企业重金招聘Python工程师标准>>> 一.要解决这个问题首先要知道json格式是什么? JSON格式: 比如学生有学号,姓名,性别等. 用json表示则为: {&q ...

  5. Java使用easyexcel读大文件

    mavn <dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</art ...

  6. 海量文件、超大文件,如何实现高速传输?

    随着云计算.大数据.物联网的发展,企业拥有的数据量呈指数级急剧增长,海量文件随之产生.由于优化业务流程.降低成本.提升效率的需要,这些原来很少使用的海量文件,成为企业从中挖掘商业价值的宝库. 如何便捷 ...

  7. 超大Sql文件_超大文件_mysql数据导入到mycat数据库_亲测好用---Linux运维工作笔记053

    对于mycat数据导入,最近一直在研究,因为有个客户的mysql数据库中,单表数据已经30多个GB, 存了上亿条的数据. 查询速度远远跟不上需求了,这个时候,我做了一个mycat集群,用了22台cen ...

  8. GeoJson的生成与解析,JSON解析,Java读写geojson,geotools读取shp文件,Geotools中Geometry对象与GeoJson的相互转换

    GeoJson的生成与解析 一.wkt格式的geometry转成json格式 二.json格式转wkt格式 三.json格式的数据进行解析 四.Java读写geojson 五.geotools读取sh ...

  9. oracle写java文件_Oracle PL/SQL java读写文件权限问题得到解决

    在ORACLE中PL/SQL利用java读取文件 参考了 的内容,但是出现如下错误: Exception in thread "Root Thread" java.security ...

最新文章

  1. 应该使用c# 预定义类型 还是绝对不要使用预定义类型。
  2. 中国固件更新软件被指盗取用户数据 遭美手机厂商替换
  3. 一款小巧好用的MAC地址扫描器
  4. php mariadb mysql.sock_(LNMP) Nginx_PHP_MariaDB
  5. 学习笔记之-Activiti7工作流引擎,概述,环境搭建,类关系图,使用Activiti BPMN visualizer,流程变量,组任务 网关,Activiti整合Spring SpringBoot
  6. java控制台高级_K9s Kubernetes的高级控制台
  7. 手把手教你用Python给小姐姐美个颜
  8. Espcms 注册或登录提示Can not connect to MySQL server
  9. 【java学习之路】(javaWeb【后端】篇)004.Thymeleaf
  10. profile和bashrc
  11. 【ECG理论篇】(2)AI实现心律失常判别:心电数据预处理
  12. 抽象代数学习笔记二《群:群的例子》
  13. 计算机专业需要外语口试,计算机专业英语词汇大全,计算机专业必读英语词汇!...
  14. Yoshua Bengio:注意力是“有意识”AI的核心要素
  15. 免费开源客户关系管理系统
  16. 对视频马赛克的调研学习报告
  17. Amigo---Android hotfix terminator
  18. API to UPDATE Oracle FND User
  19. 店铺小程序怎么做的?【小程序商城】
  20. java 发送网易邮箱邮件

热门文章

  1. Centos 云服务器磁盘占用率90%以上的排查解决
  2. flutter闪屏过渡动画,闪光占位动画
  3. Android RecyclerView(八)设置自定义 下拉刷新 与 上拉加载数据
  4. Android动态显示和隐藏状态栏
  5. java基础—FileWriter 的使用
  6. supersocket新的配置属性 textEncoding
  7. 如何让DIV模块随着页面固定和不固定
  8. 前端向后端发送请求,后端返回的一个值的请求的ajax.get();方法
  9. 设计模式(一)单例模式:5-单元素枚举类模式
  10. 用插件的形式编写升级版 jquery_select_interval.js 源码