zoukankan      html  css  js  c++  java
  • java读写大文件

    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     }

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

  • 相关阅读:
    2011/6/24 数据库分析
    项目代码总结
    背景透明 by sofish
    ie6 reflow bug
    ID与CLASS的使用技巧
    CSS浮动属性Float详解 by 帕兰
    javascript闭包 by 李松峰
    详解CSS选择器、优先级与匹配原理
    垂直对齐:verticalalign属性 by ddcatlee
    行高lineheight,以及基线、顶线、中线和底线,还有内容区域、行内框和行框 by 豆豆猫的窝
  • 原文地址:https://www.cnblogs.com/dengguangxue/p/10570933.html
Copyright © 2011-2022 走看看