Java读取文件的几种方式

package com.mesopotamia.test;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
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;
import java.util.Scanner;

import org.apache.log4j.Logger;
/*
 * 原文学习请加微信订阅号:it_pupil
 * **/
public class FileRead {
	private static Logger logger = Logger.getLogger(FileRead.class);
	public static void main(String args[]) throws FileNotFoundException{
		String path = "C:" + File.separator + "test" + File.separator + "Alice.txt";
		readFile3(path);
	}

	public static void readFile(String path) throws FileNotFoundException {
		long start = System.currentTimeMillis();//开始时间
		int bufSize = 1024;//1K缓冲区
		File fin = new File(path);
		/*
		 * 通道就是为操作文件而建立的一个连接。(读写文件、内存映射等)
		 * 此处的getChannel()可以获取通道;
		 * 用FileChannel.open(filename)也可以创建一个通道。
		 * "r"表示只读。
		 *
		 * RandomAccessFile是独立与I/O流家族的类,其父类是Object。
		 * 该类因为有个指针可以挪动,所以,可以从任意位置开始读取文件数据。
		 * **/
		FileChannel fcin = new RandomAccessFile(fin, "r").getChannel();
		//给字节缓冲区分配大小
		ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);
		String enterStr = "\n";
		try {
			byte[] bs = new byte[bufSize];
			String tempString = null;
			while (fcin.read(rBuffer) != -1) {//每次读1k到缓冲区
				int rSize = rBuffer.position();//记录缓冲区当前位置
				rBuffer.rewind();//位置归零,标记取消,方便下次循环重新读入缓冲区。
				rBuffer.get(bs);//将缓冲区数据读到字节数组中
				rBuffer.clear();//清除缓冲
				/*
				 * 用默认编码将指定字节数组的数据构造成一个字符串
				 * bs:指定的字节数组,0:数组起始位置;rSize:数组结束位置
				 * */
				tempString = new String(bs, 0, rSize);
				int fromIndex = 0;//每次读的开始位置
				int endIndex = 0;//每次读的结束位置
				//按行读String数据
				while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
					String line = tempString.substring(fromIndex, endIndex);//转换一行
					System.out.print(line);
					fromIndex = endIndex + 1;
				}
			}
            long end = System.currentTimeMillis();//结束时间
            System.out.println("传统IO读取数据,指定缓冲区大小,总共耗时:"+(end - start)+"ms");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void readFile1(String path) {
    	long start = System.currentTimeMillis();//开始时间
        File file = new File(path);
        if (file.isFile()) {
        	/*使用Reader家族,表示我要读字符数据了,
        	 *使用该家族中的BufferedReader,表示我要建立缓冲区读字符数据了。
        	 * */
            BufferedReader bufferedReader = null;
            FileReader fileReader = null;
            try {
                fileReader = new FileReader(file);
                //嵌套使用,装饰者模式,老生常谈。装饰者模式的使用,可以读前面小砖写的《从熏肉大饼到装饰者模式》
                bufferedReader = new BufferedReader(fileReader);
                String line = bufferedReader.readLine();
                //一行一行读
                while (line != null) { //按行读数据
                    System.out.println(line);
                    line = bufferedReader.readLine();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
            	//最后一定要关闭
                try {
                    fileReader.close();
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                long end = System.currentTimeMillis();//结束时间
                System.out.println("传统IO读取数据,不指定缓冲区大小,总共耗时:"+(end - start)+"ms");
            }  

        }
    } 

	public static void readFile3(String path) {
		long start = System.currentTimeMillis();//开始时间
        long fileLength = 0;
        final int BUFFER_SIZE = 0x300000;// 3M的缓冲
            File file = new File(path);
            fileLength = file.length();
            try {
            	/*使用FileChannel.map方法直接把整个fileLength大小的文件映射到内存中**/
                MappedByteBuffer inputBuffer = new RandomAccessFile(file, "r").getChannel()
                	.map(FileChannel.MapMode.READ_ONLY, 0, fileLength);// 读取大文件
                byte[] dst = new byte[BUFFER_SIZE];// 每次读出3M的内容
                //每3M做一个循环,分段将inputBuffer的数据取出。
                for (int offset = 0; offset < fileLength; offset += BUFFER_SIZE) {
                	//防止最后一段不够3M
                    if (fileLength - offset >= BUFFER_SIZE) {
                    	//一个字节一个字节的取出来放到byte[]数组中。
                        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("XXX");
                    //hasNext()所参照的token就是上面的XXX
                    while (scan.hasNext()) {
                        // 这里为对读取文本解析的方法
                        System.out.print(scan.next() + "XXX");
                    }
                    scan.close();
                }
                System.out.println();
                long end = System.currentTimeMillis();//结束时间
                System.out.println("NIO 内存映射读大文件,总共耗时:"+(end - start)+"ms");
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

  

时间: 2024-08-27 06:16:49

Java读取文件的几种方式的相关文章

关于java读取文件的几种方式

摘自:http://jaczhao.iteye.com/blog/1616716 Java代码 public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.out.prin

Java读写文件的几种方式

自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行三种方式来进行文件的操作. import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.F

java复制文件的4种方式

1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是第一个方法的代码: private static void copyFileUsingFileStreams(File source, File dest)         throws IOException {         InputStream input = null;         

FileReader读取文件的三种方式

package com.agoly.test; //import java.io.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileTest { public static void main(String[] args) { // 读取文件的方式一:逐个字符来读取文本文件 FileReader fr = null; try { fr

shell读取文件的几种方式

案例文本文件 [[email protected]01 ~]# cat a.txt ID name gender age email phone 1 Bob male 28 [email protected] 18023394012 2 Alice female 24 [email protected] 18084925203 3 Tony male 21 [email protected]163.com 17048792503 4 Kevin male 21 [email protected]

Java 读取文件的几种方法

1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 5.将内容追加到文件尾部 /*** 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件.*/ public class ReadFromFile { public static void readFileByBytes(String fileName) { File file = new File(fileName); InputStream in = null; try { System.o

java写入文件的三种方式比较

1.FileOutputStream方式 2.BufferedOutputStream方式 3.FileWriter方式 经过多次测试,发现不缓存的FileOutputStream会比较慢,当文件小的话,关系不大,但是当文件大时,消耗的时间就会有很明显差别 package fileTest; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.

java 读取文件内容 三种形式及效率对比

IOUtils.getStringFromReader() 读取方式为最快的 InputStream in = null; String line = ""; long start=0,end=0; try { start = System.currentTimeMillis(); in = new FileInputStream(new File("D://1.txt")); InputStreamReader stream = new InputStreamRe

java 选择文件的两种方式

第一种 JFileChooser JLabel lblNewLabel_1 = new JLabel(""); JFileChooser jf=new JFileChooser(); jf.setDialogTitle("选择头像"); jf.setFileFilter(new FileFilter() { @Override public String getDescription() { // TODO Auto-generated method stub re