大文件读取java
A. java如何读取大容量的txt文件
java读取txt文件内容。可以作如下理解:
首先获得一个文件句柄。File file = new File(); file即为文件句柄。两人之间连通电话网络了。接下来可以开始打电话了。
通过这条线路读取甲方的信息:new FileInputStream(file) 目前这个信息已经读进来内存当中了。接下来需要解读成乙方可以理解的东西
既然你使用了FileInputStream()。那么对应的需要使用InputStreamReader()这个方法进行解读刚才装进来内存当中的数据
解读完成后要输出呀。那当然要转换成IO可以识别的数据呀。那就需要调用字节码读取的方法BufferedReader()。同时使用bufferedReader()的readline()方法读取txt文件中的每一行数据哈。
B. java 读取一个巨大的文本文件,该如何实现 既能保证内存不溢出 又能保证性能
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class ReadBig {
 public static String fff = "C:\\mq\\read\\from.xml";
 public static void main1(String[] args) throws Exception {
  final int BUFFER_SIZE = 0x300000;// 缓冲区大小为3M
  File f = new File(fff);
  /**
   * 
   * map(FileChannel.MapMode mode,long position, long size)
   * 
   * mode - 根据是按只读、读取/写入或专用(写入时拷贝)来映射文件,分别为 FileChannel.MapMode 类中所定义的
   * READ_ONLY、READ_WRITE 或 PRIVATE 之一
   * 
   * position - 文件中的位置,映射区域从此位置开始;必须为非负数
   * 
   * size - 要映射的区域大小;必须为非负数且不大于 Integer.MAX_VALUE
   * 
   * 所以若想读取文件后半部分内容,如例子所写;若想读取文本后1/8内容,需要这样写map(FileChannel.MapMode.READ_ONLY,
   * f.length()*7/8,f.length()/8)
   * 
   * 想读取文件所有内容,需要这样写map(FileChannel.MapMode.READ_ONLY, 0,f.length())
   * 
   */
  MappedByteBuffer inputBuffer = new RandomAccessFile(f, "r")
    .getChannel().map(FileChannel.MapMode.READ_ONLY,
      f.length() / 2, f.length() / 2);
  byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容
  long start = System.currentTimeMillis();
  for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) {
   if (inputBuffer.capacity() - offset >= BUFFER_SIZE) {
    for (int i = 0; i < BUFFER_SIZE; i++)
     dst[i] = inputBuffer.get(offset + i);
   } else {
    for (int i = 0; i < inputBuffer.capacity() - offset; i++)
     dst[i] = inputBuffer.get(offset + i);
   }
   int length = (inputBuffer.capacity() % BUFFER_SIZE == 0) ? BUFFER_SIZE
     : inputBuffer.capacity() % BUFFER_SIZE;
   System.out.println(new String(dst, 0, length));// new
   // String(dst,0,length)这样可以取出缓存保存的字符串,可以对其进行操作
  }
  long end = System.currentTimeMillis();
  System.out.println("读取文件文件一半内容花费:" + (end - start) + "毫秒");
 }
 public static void main2(String[] args) throws Exception {
  int bufSize = 1024;
  byte[] bs = new byte[bufSize];
  ByteBuffer byteBuf = ByteBuffer.allocate(1024);
  FileChannel channel = new RandomAccessFile(fff, "r").getChannel();
  while (channel.read(byteBuf) != -1) {
   int size = byteBuf.position();
   byteBuf.rewind();
   byteBuf.get(bs); // 把文件当字符串处理,直接打印做为一个例子。
   System.out.print(new String(bs, 0, size));
   byteBuf.clear();
  }
 }
 public static void main(String[] args) throws Exception {
  BufferedReader br = new BufferedReader(new FileReader(fff));
  String line = null;
  while ((line = br.readLine()) != null) {
   System.out.println(line);
  }
 }
}
C. java 读取大容量文件,内存溢出怎么分段读取(按一定容量读取)
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class TestPrint {
	public static void main(String[] args) throws IOException {
		String path = "你要读的文件的路径";
		RandomAccessFile br=new RandomAccessFile(path,"rw");//这里rw看你了。要是之都就只写r
		String str = null, app = null;
		int i=0;
		while ((str = br.readLine()) != null) {
			i++;
			app=app+str;
			if(i>=100){//假设读取100行
				i=0;
//				这里你先对这100行操作,然后继续读
app=null;
			}
		}
		br.close();
	}
}
D. JAVA怎么快速读取一个大文件(1G多有500万行)
java NIO除了异步非阻塞特性外,另外一个重要特性就是文件读取,原理是文件地址直接映射在线程内存,不要经过操作系统,可以大大提高响应速度。
下面是例子:
http://blog.csdn.net/chenleixing/article/details/44207469
