问题描述
java读取超大文件,(5G左右),并且每行读出来,需要处理下再重新写入一个文件,大家有碰到过的吗?求指点
解决方案
import java.io.*;public class ReadBigFileLineByLine{ public static void main(String[] args) { try{ //Big file to read String fileName = "MyBigFile.txt"; FileReader fileReader = new FileReader(fileName); BufferedReader bufferedReader = new BufferedReader(fileReader); String oneLine; while ((oneLine = bufferedReader.readLine()) != null) { System.out.println(oneLine); } bufferedReader.close(); }catch(Exception e){ System.out.println("Error while reading file:" + e.getMessage()); } }}
解决方案二:
大文件还是用内存映射吧,从FileChannel得到map来处理,代码就不贴了,一搜一大堆,没什么复杂的,你想想5g的文件,少做一次拷贝,效率会提高多少。。。
解决方案三:
读大文件一般用RandomAccessFile下手吧
解决方案四:
搞了半天的测试数据 数据量过大 电脑都卡住啦半天不能动 找了个类似的例子 看看对你有没有电帮助import java.io.*;import java.util.*;import java.io.FileInputStream;import java.io.FileOutputStream;import java.lang.String;import java.awt.*;////// Red//// public class Red {public static void main(String []args) throws IOException{ // int aac; //输出原文件 System.out.println("Program Beginning ... ..."); FileInputStream inStream = new FileInputStream("demo.txt"); //文件放在本程序的 int inread = inStream.available(); //同一文件目录下 byte inword[] = new byte[inread]; int bytesRead = inStream.read(inword,0,inread); System.out.println(new String(inword,0)); char ttc[] = new char[inread+2]; String readouts[] = new String[10]; System.out.println("..............................."); int cline = 0 ; int bline = 0 ; int eline = 0 ; //一次取一行将原文件内容归纳成按行存储的输入流 ttc[inread] = 13; ttc[inread+1] = 10; for(int i = 0 ; i < inread ; i++) { ttc[i] = (char) inword[i]; System.out.print(ttc[i]); if(ttc[i] == 10 || i == (inread-1)) { eline = i; if(i == (inread-1)) readouts[cline] = new String(ttc,bline,(eline-bline+3)); else readouts[cline] = new String(ttc,bline,(eline-bline+1)); bline = i+1; cline++; } } System.out.println(); System.out.println("..............................."); inStream.close(); //将每行的内容在屏幕上显示一遍 for(int l = 0 ; l < cline ; l++) { System.out.print(readouts[l]); } System.out.println(); //输入一空行。 //compare 按字典顺序对比,并按照顺序排序 String abba = new String(); for(int bn = 0 ; bn < cline-1 ; bn++) { for(int m = 0 ; m < cline-1-bn ; m++) { int duibi = readouts[m].compareTo(readouts[m+1]); if(duibi > 0 ) { abba = readouts[m]; readouts[m] = readouts[m+1]; readouts[m+1] = abba; } } } System.out.println("This documents 'flow' has been sort now..."); // cout the sort string 输出排好顺序的每行内容 for(int l = 0 ; l < cline ; l++) { System.out.print(readouts[l]); } ///write string in txt 输出一行...号 System.out.println("................."); // cout if documents exist判断文件是否存在,不存在则创建一新文件 File testtxt = new File(""); if(testtxt.exists() == false) { System.out.println("d.txt is not exist"); System.out.println("Creat new documents now....waiting....."); testtxt = new File("d.txt"); } else System.out.println("Have found the document " d.txt" "); if(testtxt.exists() == true) System.out.println("Creat "d.txt" successly...Congratulation!!!"); //利用新创立的文件,创建一个输出流,以方便写入文件 FileOutputStream outStream = new FileOutputStream("d.txt"); String houl = new String(); //将排好顺序的内容合并到一个字符串中 for(int i = 0 ; i < cline ; i++) { houl = houl+readouts[i]; } //打印该字符串 houl = houl+'n'; System.out.println(houl); System.out.println("begin store these data..."); //将此字符串的内容写入输出流所代表的文件 for (int i = 0 ; i < inread ; i++) { outStream.write(houl.charAt(i)); } //关闭输出流 outStream.close(); //创建一新的输入流,并在屏幕显示输入的内容。 System.out.println("This documents stored ok..!~~"); System.out.println("Program Beginning ... ..."); FileInputStream inStreamnew = new FileInputStream("d.txt"); int inreadnew = inStreamnew.available(); byte inwordnew[] = new byte[inreadnew]; int bytesReadnew = inStreamnew.read(inwordnew,0,inreadnew); System.out.println("This is a test for this program..."); System.out.println(new String(inwordnew,0)); //关闭输入流 inStreamnew.close();}}