最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(char[] ch,int off,int length),flush()和close()方法为抽象方法,Reader中read(char[] ch,int off,int length)和close()方法是抽象方法。子类应该分别实现他们。
按制定的字节进行读取文件数据,具体方法代码如下:
/** * 按制定的字节进行读取数据 */ public static void readFileByte(String fileName){ int bufSize = 1024; byte[] br = new byte[bufSize]; ByteBuffer buffer = ByteBuffer.allocate(1024); FileChannel channel = null; try { channel = new RandomAccessFile(fileName, "r").getChannel(); while (channel.read(buffer) != -1) { int size = buffer.position(); buffer.rewind(); buffer.get(br);//:把文件当字符串处理,直接打印做为一个例子。 System.out.println(new String(br,0,size)); buffer.clear();//:清屏 count++; } } catch (Exception e) { e.printStackTrace(); } System.err.println("文件读取时每" + bufSize + "字节读取一次,共读取" + count + "次数"); }
按行数进行读取几G文件数据,具体方法代码如下:
/** * 按行数读取数据 * @param fileName */ public static void readFileLine(String fileName){ BufferedReader buffer = null; String line = null; try { buffer = new BufferedReader(new FileReader(fileName)); while ((line = buffer.readLine()) != null) { count++; System.out.println(line); } System.err.println("共有" + count + "行数据"); } catch (Exception e) { e.printStackTrace(); } }
测试方法,具体代码如下:
package test; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class ReadBigFile { public static int count = 0;// :记录数据 /** * 主函数 */ public static void main(String[] args) { String fileName = "d://test.xml"; //readFileLine(fileName); readFileByte(fileName); //readFileHalf(fileName); } public static void readFileHalf(String fileName){ final int BUFFER_SIZE = 0x300000;// 缓冲区大小为3M File file = new File(fileName); /** * 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 = null; try { inputBuffer = new RandomAccessFile(file, "r") .getChannel().map(FileChannel.MapMode.READ_ONLY, file.length() / 2, file.length() / 2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容 long start = System.currentTimeMillis(); for (int offset = 0; offset < inputBuffer.capacity(); offset += BUFFER_SIZE) { count++; 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("读取文件文件一半内" + count + "容花费:" + (end - start) + "毫秒"); } }