文件各种读取方式

来源:互联网 发布:如何评价特朗普 知乎 编辑:程序博客网 时间:2024/05/22 01:38

最近工作需要正好需要研究文件的上传下载,由于基础打得不算扎实,实在忏愧,还是需要恶补一下,文件的各种读入方式。

File 类关注的是文件在磁盘上的存储,而FileInputStream流类关注的是文件的内容。
FileInputStream 用于从文件读取信息 代表文件名的一个 String,或者一个
File 或 FileDescriptor 对 象 / 作 为 一 个 数 据 源 使 用 。 通 过 将 其 同 一 个
FilterInputStream 对象连接,可提供一个有用的接口。

1、InputStream–>read 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。

import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;public class ReadByByte {    public static void main(String args[]) throws IOException {        File file = new File("C:/Users/kuangww/Desktop/tes  t.txt");            InputStream in = null;        try {            in = new FileInputStream(file);            int tempByte;            System.out.println("以字节为单位读取文件内容,一次读一个字节:");            long start1 = System.nanoTime();            while ((tempByte = in.read()) != -1) {                System.out.write(tempByte);            }            System.out.println("1----" + (System.nanoTime() - start1)); //一次读一个字节,耗费时间            in.close();//通常不使用close会导致内存泄露,垃圾回收机制会回收,但是最好自己显式关闭,这并不是特别关键。        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();            return;        }        System.out.println("以字节为单位读取文件内容,一次读多个字节:");        byte[] tempbytes = new byte[100];        in = new FileInputStream(file);        int byteRead;        long start2 = System.nanoTime();        while ((byteRead = in.read(tempbytes)) != -1) {            System.out.write(tempbytes, 0, byteRead);        }        //一次读多个字节,耗费时间        System.out.println("2----" + (System.nanoTime()  - start2));         in.close();    }}

输入结果:

以字节为单位读取文件内容,一次读一个字节:helloworldkuangww1----308540以字节为单位读取文件内容,一次读多个字节:helloworldkuangww2----31031

通过上面的示例可以,多个字节读取,效率更高。因为单个字节读取,线程阻塞占用的时间更多。可通过FileInputStream的available()返回实际可读的字节数,如果available用于read()==-1之后,获得的字节数为0.

2、BufferedReader–>readLine 以行为单位读取文件,常于读取面向行的格式文件

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class ReadByLine {    public static void main(String[] args) {        File file = new File("C:/Users/kuangww/Desktop/test.txt");        BufferedReader reader = null;        try {            System.out.println("以行为单位读取文件内容,一次读一整行:");            reader = new BufferedReader(new FileReader(file));            String tempStr = null;            int line = 1;            while ((tempStr = reader.readLine()) != null) {                System.out.println("line " + line + ": " + tempStr);                line++;            }            reader.close();        } catch (IOException e) {            e.printStackTrace();        } finally {            if (reader != null) {                try {                    reader.close();                } catch (IOException e1) {                }            }        }    }}

輸出結果:

line 1: helloline 2: line 3: worldline 4: line 5: kuangww

3、RandomAccessFile 随机读取文件内容

4、Reader 以字符为单位读取文件,常用于读文本,数字等类型的文件

本文參照:http://www.cnblogs.com/fnlingnzb-learner/p/6011324.html 非常感謝