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 }
共同学习,共同进步,若有补充,欢迎指出,谢谢!