原文地址:http://wgslucky.blog.163.com/blog/static/97562532201332324639689/
java 读取一个巨大的文本文件既能保证内存不溢出又能保证性能
1 public static void main2(String[] args) throws Exception { 2 int bufSize = 1024; 3 byte[] bs = new byte[bufSize]; 4 ByteBuffer byteBuf = ByteBuffer.allocate(1024); 5 FileChannel channel = new RandomAccessFile(fff, "r").getChannel(); 6 while (channel.read(byteBuf) != -1) { 7 int size = byteBuf.position(); 8 byteBuf.rewind(); 9 byteBuf.get(bs); // 把文件当字符串处理,直接打印做为一个例子。 10 System.out.print(new String(bs, 0, size)); 11 byteBuf.clear(); 12 } 13 14 }
1 public static void main(String[] args) throws Exception { 2 BufferedReader br = new BufferedReader(new FileReader(fff)); 3 String line = null; 4 while ((line = br.readLine()) != null) { 5 System.out.println(line); 6 } 7 }
1 public static void main(String[] args) throws Exception { 2 int bufSize = 1024; 3 byte[] bs = new byte[bufSize]; 4 ByteBuffer byteBuf = ByteBuffer.allocate(1024); 5 FileChannel channel = new RandomAccessFile("d:\filename","r").getChannel(); 6 while(channel.read(byteBuf) != -1) { 7 int size = byteBuf.position(); 8 byteBuf.rewind(); 9 byteBuf.get(bs); 10 // 把文件当字符串处理,直接打印做为一个例子。 11 System.out.print(new String(bs, 0, size)); 12 byteBuf.clear(); 13 } 14 } 15 16 }
java 读取大容量文件,内存溢出?怎么按几行读取,读取多次
1 import java.io.BufferedReader; 2 import java.io.FileNotFoundException; 3 import java.io.FileReader; 4 import java.io.IOException; 5 import java.io.RandomAccessFile; 6 import java.util.Scanner; 7 8 public class TestPrint { 9 public static void main(String[] args) throws IOException { 10 String path = "你要读的文件的路径"; 11 RandomAccessFile br=new RandomAccessFile(path,"rw");//这里rw看你了。要是之都就只写r 12 String str = null, app = null; 13 int i=0; 14 while ((str = br.readLine()) != null) { 15 i++; 16 app=app+str; 17 if(i>=100){//假设读取100行 18 i=0; 19 // 这里你先对这100行操作,然后继续读 20 app=null; 21 } 22 } 23 br.close(); 24 } 25 26 }
当逐行读写大于2G的文本文件时推荐使用以下代码
1 void largeFileIO(String inputFile, String outputFile) { 2 try { 3 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(inputFile))); 4 BufferedReader in = new BufferedReader(new InputStreamReader(bis, "utf-8"), 10 * 1024 * 1024);//10M缓存 5 FileWriter fw = new FileWriter(outputFile); 6 while (in.ready()) { 7 String line = in.readLine(); 8 fw.append(line + " "); 9 } 10 in.close(); 11 fw.flush(); 12 fw.close(); 13 } catch (IOException ex) { 14 ex.printStackTrace(); 15 }
jdk本身就支持超大文件的读写
网上的文章基本分为两大类,一类是使用BufferedReader类读写超大文件;另一类是使用RandomAccessFile类读取,经过比较,最后使用了前一种方式进行超大文件的读取,下面是相关代码,其实很简单
1 File file = new File(filepath); 2 BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file)); 3 BufferedReader reader = new BufferedReader(new InputStreamReader(fis,"utf-8"),5*1024*1024);// 用5M的缓冲读取文本文件 4 5 String line = ""; 6 while((line = reader.readLine()) != null){ 7 //TODO: write your business 8 }