Java读取文件 利用MappedByteBuffer进行缓冲,这样可以保证边读取大文件,边进行处理
package sean; import java.io.ByteArrayInputStream; import java.io.File; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.util.Scanner; public class Snippet { public void readResource() { long fileLength = 0; final int BUFFER_SIZE = 0x300000;// 3M的缓冲 //for (String fileDirectory : this.readResourceDirectory())// 得到文件存放路径,我这里使用了一个方法从XML文件中读出文件的 // 存放路径,当然也可以用绝对路径来代替这里的fileDriectory //{ File file = new File("D:\\MyData.txt"); fileLength = file.length(); try { MappedByteBuffer inputBuffer = new RandomAccessFile(file, "r") .getChannel().map(FileChannel.MapMode.READ_ONLY, 0, fileLength);// 读取大文件 byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容 for (int offset = 0; offset < fileLength; offset += BUFFER_SIZE) { if (fileLength - offset >= BUFFER_SIZE) { for (int i = 0; i < BUFFER_SIZE; i++) dst[i] = inputBuffer.get(offset + i); } else { for (int i = 0; i < fileLength - offset; i++) dst[i] = inputBuffer.get(offset + i); } // 将得到的3M内容给Scanner,这里的XXX是指Scanner解析的分隔符 Scanner scan = new Scanner(new ByteArrayInputStream(dst)) .useDelimiter(" "); while (scan.hasNext()) { // 这里为对读取文本解析的方法 System.out.print(scan.next() + " "); } scan.close(); } } catch (Exception e) { e.printStackTrace(); } //} } public static void main(String[] args) { Snippet sp = new Snippet(); sp.readResource(); } }
时间: 2024-10-19 05:02:52