java读写大文件

摘要:
Java读取和写入2G以上的大型文件(建议使用以下方法)1 staticStringsourceFilePath=“H:DataSource-ready-question.json”;2staticStringdistFilePath=“H:\DataSource就绪\分离\”;34 publicstaticvoidmain(String[]参数)5{6SimpleDateForma

java读写2G以上的大文件(推荐使用以下方法)

 1     static String sourceFilePath = "H:\DataSource-ready\question.json" ;
 2     static String distFilePath = "H:\DataSource-ready\separate\" ;
 3     
 4     public static void main( String[] args )
 5     {
 6         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
 7         System.out.println("开始时间" + sdf.format(new Date()));// new Date()为获取当前系统时间
 8 
 9         long timer = System.currentTimeMillis();         
10         try {
11             File file = new File(sourceFilePath);   
12             BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));    
13             BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),7*1024*1024);// 用7M的缓冲读取文本文件  
14               
15             int count = 1 ;
16             String outputFile = distFilePath + count + ".json" ;
17             FileWriter fw = new FileWriter(outputFile);
18             String line = "";
19             while((line = reader.readLine()) != null){
20                 if ( line.contains("},") ) {
21                     line = line.replace("},", "}") ;
22                     fw.append(line + "
");
23                     fw.flush();
24                     fw.close();
25                     count ++ ;
26                     outputFile = distFilePath + count + ".json" ;
27                     fw = new FileWriter(outputFile);
28                 }
29                 else {
30                     fw.append(line + "
");    
31                 }                            
32             }
33             reader.close();       
34             fw.flush();
35             fw.close();
36         } catch (IOException e) {
37             System.out.println("读写文件失败!");
38             e.printStackTrace();
39         }
40         timer = System.currentTimeMillis() - timer;  
41         System.out.println("处理时间:" + timer + " 毫秒");  
42         System.out.println("结束时间:" + sdf.format(new Date()));
43     }

共同学习,共同进步,若有补充,欢迎指出,谢谢!

免责声明:文章转载自《java读写大文件》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇关于文件中的0D、0A登录接口需html中的token时,需用requests-html库下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JSP上传视频等大文件

前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方式来学习文件上传的原理与实践。该博客重在实践。 一. Http协议原理简介      HTTP是一个属于应用层的面向对象的协议,由于其简捷、快速的方式,适用于分布式超媒体信息系统。它于19...

python mock接口返回数据(转载)

Python mock  在测试过程中,为了更好地展开单元测试,mock一些数据跟对象在所难免,下面讲一下python的mock的简单用法。 关于python mock,网上有很多资料,这里不会讲的特别深,但一定会是实用为主,看完后,至少可以让你知道mock是怎样用的。 1.mock对象方法中的返回数据: 我们经常会需要这样的场景,a系统跟b系统联调,b...

spring boot 重定向

/** * 测试各个html文件用。 * @param model * @return */ @RequestMapping("home") public String home(RedirectAttributes model) { model.addAttribute("...

LeetCode 51. N-QueensN皇后 (C++)(八皇后问题)

题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to th...

rabbitMQ第二篇:java简单的实现RabbitMQ

前言:在这里我将用java来简单的实现rabbitMQ。下面我们带着下面问题来一步步的了解和学习rabbitMQ。 1:如果消费者连接中断,这期间我们应该怎么办 2:如何做到负载均衡 3:如何有效的将数据发送到相关的接收者?就是怎么样过滤 4:如何保证消费者收到完整正确的数据 5:如何让优先级高的接收者先收到数据 一:"Hello RabbitMQ" 下面...

cjson库的使用以及源码阅读

cjson是一个用c语言开发的json解析库,免费开源只有一个c文件和一个h文件。json和xml功能相似,可以用来传输数据,存储数据以及表达程序当前的状态。 1、下载cjson的源码         https://github.com/DaveGamble/cJSON 2、阅读readme文件可以大概的了解一下cjson的介绍以及使用方法,我尝试着把...