java基础I/O

1,I/O流的概念:

IO流代表的是一个数据输入的源或者输出的目标地址,可以是硬盘,内存,网络或者什么其他的电子设备,而IO流的类型也很多比如最简单的字节或者字符,或者其他更高级的对象。

不管它有多少特性,本质上 流就是一组字节序列,或者直接可以说01字节序列吧?

2,字节流

首先是一个官网例子:CopyBytes.java。

java 复制代码
import java.io.*;

public class CopyBytes {
    public static void main(String[] args) throws IOException {
        read(); //420856988
        read2(); //1100868
        readBuffer(); //5977046
        readBuffer2(); //964845
    }

    public static void read() throws IOException {
        final long l = System.nanoTime();
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            String properties = System.getProperty("java.class.path");
            in = new FileInputStream(properties+"/xanadu.txt"); //想要在IDEA里面跑起来 就把文件放在resources里面
            //in = new FileInputStream("xanadu.txt"); //否则把文件放在classpath目录里面,使用java命令启动程序
            out = new FileOutputStream(properties+"/outagain.txt");
            //out = new FileOutputStream("outagain.txt");
            int c;

            while ((c = in.read()) != -1) { //这里每次读取一个字节
                out.write(c);
            }
        } finally {
            if (in != null) {
                in.close();//必须得关闭
            }
            if (out != null) {
                out.close();
            }
        }
        System.out.println(System.nanoTime() - l);
    }

    public static void read2() throws IOException {
        final long l = System.nanoTime();
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            String properties = System.getProperty("java.class.path");
            in = new FileInputStream(properties+"/xanadu.txt"); //想要在IDEA里面跑起来 就把文件放在resources里面
            //in = new FileInputStream("xanadu.txt"); //否则把文件放在classpath目录里面,使用java命令启动程序
            out = new FileOutputStream(properties+"/outagain.txt");
            //out = new FileOutputStream("outagain.txt");
            int c;
            byte[] bufferBytes = new byte[8192];
            while ((c = in.read(bufferBytes)) != -1) { //这里每次读取一个字节
                out.write(bufferBytes,0,c);
            }
        } finally {
            if (in != null) {
                in.close();//必须得关闭
            }
            if (out != null) {
                out.close();
            }
        }
        System.out.println(System.nanoTime() - l);
    }

    public static void readBuffer() throws IOException {
        final long l = System.nanoTime();
        InputStream in = null;
        OutputStream out = null;

        try {
            String properties = System.getProperty("java.class.path");
            in = new BufferedInputStream(new FileInputStream(properties+"/xanadu.txt")); //想要在IDEA里面跑起来 就把文件放在resources里面
            out = new BufferedOutputStream(new FileOutputStream(properties+"/outagain.txt"));
            int c;

            while ((c = in.read()) != -1) { //这里每次读取一个字节
                out.write(c);
            }
        } finally {
            if (in != null) {
                in.close();//必须得关闭
            }
            if (out != null) {
                out.close();
            }
        }
        System.out.println(System.nanoTime() - l);
    }

    public static void readBuffer2() throws IOException {
        final long l = System.nanoTime();
        InputStream in = null;
        OutputStream out = null;

        try {
            String properties = System.getProperty("java.class.path");
            in = new BufferedInputStream(new FileInputStream(properties+"/xanadu.txt")); //把数据批量读进内存
            out = new BufferedOutputStream(new FileOutputStream(properties+"/outagain.txt"));
            int c;
            byte[] bufferBytes = new byte[8192];
            while ((c = in.read(bufferBytes)) != -1) { //这个批量读进bufferBytes数组。
                out.write(bufferBytes,0,c);
            }
            out.flush(); //这里可以手动刷新一下,防止数据丢失。
        } finally {
            if (in != null) {
                in.close();//必须得关闭
            }
            if (out != null) {
                out.close();
            }
        }
        System.out.println(System.nanoTime() - l);
    }
}

2.1 字节流默认每次输入或者输出一个字节(8-bit),类很多,但是所有的都是InputStream/OutputStream的子类,CopyBytes.java使用了FileInputStream和FileOutputStream,但是其他的类的使用方法差不多,除了构造方法不一样。

2.2 整个复制的过程比较简单,就是在输入流里面读取一个字节,然后往输出流里面写一个字节,直到读完。

2.3 输入输出流使用完毕后必须关闭,而且是在finally代码块里面确保一定关闭,防止资源泄漏。

2.4 这个CopyBytes看起来挺好用,但这只是一个低级别的I/O操作,平时应该避免使用,它只是基础,如果复制可以使用Character Stream。

3,字符流

首先是官网的两个例子:CopyCharacters.java和CopyLine.java

java 复制代码
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.IOException;

public class CopyLines {
    public static void main(String[] args) throws IOException {

        BufferedReader inputStream = null;
        PrintWriter outputStream = null;

        try {
            String properties = System.getProperty("java.class.path");
            System.out.println(properties);
            inputStream = new BufferedReader(new FileReader(properties+"/xanadu.txt"));
            outputStream = new PrintWriter(new FileWriter(properties+"/characteroutput1.txt"));

            String l;
            while ((l = inputStream.readLine()) != null) {
                outputStream.println(l);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
    }
}
java 复制代码
import java.io.*;

public class CopyCharacters {
    public static void main(String[] args) throws IOException {
        readByReader();       //27623168
        readByBufferReader(); //2098684 //2Kb的文件就相差10倍

    }

    public static void readByReader() throws IOException{
        final long l = System.nanoTime();
        Reader inputStream = null;
        Writer outputStream = null;

        try {
            String properties = System.getProperty("java.class.path");
            inputStream = new FileReader(properties+"/xanadu.txt");
            outputStream = new FileWriter(properties+"/characteroutput.txt");

            int c;
            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
        System.out.println(System.nanoTime() - l);
    }

    public static void readByBufferReader() throws IOException{

        final long l = System.nanoTime();

        Reader inputStream = null;
        Writer outputStream = null;

        try {
            String properties = System.getProperty("java.class.path");
            inputStream = new BufferedReader(new FileReader(properties+"/xanadu.txt"),8192); //这个size大小对性能的影响很大
            outputStream = new BufferedWriter(new FileWriter(properties+"/characteroutput.txt"),8192);

            int c;
            while ((c = inputStream.read()) != -1) {
                outputStream.write(c);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        }
        System.out.println(System.nanoTime() - l);
    }

}

3.1 Java默认使用Unicode存储字符值,而字符流会自动处理本地字符集与内部之间的转换。使用字符流替代字节流的程序会自动回应本地字符集而无需程序员操心。

3.2 所有的字符流都继承自Reader和Writer第一个例子CopyCharacters.java使用了FileRead和FileWriter

3.3 字符流通常是字节流的包装器,字节流执行物理I/O,而字符流处理字节与字符之间的转换,有两个通用的字节到字符"桥接"流: InputStreamReader 和 OutputStreamWriter,自定义是可以用这两个类。

3.4 字符流通常一次会读取多个字符,比如一行字符,CopyLine.java 就是按行读取写入的例子,一串带有行终止符的字符称为一行。行终止符有 回车符/换行符序列("/r/n") 单个回车符("/r") 单个换行符("/n")。

3.5 调用 readLine 将返回一行带有该行的文本,CopyLines 输出每一行 (println) ,这将附加当前操作系统的行终止符。这可能与输入文件中使用的行终止符不同

4,缓冲流

前面介绍的字节流和字符流没有缓冲,每次读取都是由底层操作系统直接操作,效率较低,因为每次操作都可能触发磁盘IO或者网络活动,为了避免这种情况设计了缓冲流。

4.1 缓冲输入流从缓冲区(内存)读取数据,当缓冲区为空是才调用底层操作系统,同样的输出流也是先输出到缓冲区,要满时才才调用本地的API。

4.2 有四个缓冲流类用于包装未缓冲的流: BufferedInputStream 和 BufferedOutputStream 创建缓冲字节流,而和 BufferedReader BufferedWriter 创建缓冲字符流。

4.3 用法:inputStream = new BufferedReader(new FileReader("xanadu.txt"));

outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));

4.4 靠,这个性能差别还是很大的,尤其是对于字节流来说可以提升近百倍。

4.5 每次创建时会创建一个固定大小的缓冲区数组(字符流是char[8192],字节流是byte[8192]),根据文档描述 默认的大小就可以满足绝大多数情况,当然我想应该是文件越大越要提高数值吧,资源允许的话。8192->8M大小

4.6 缓冲区是内存里面存储该文件的地方,在copy时还可以创建缓冲数组提高效率,最终版应该是 CopyBytes.readBuffer2()方法。

这个地方打比方的话就是 如果使用单纯的字节流,不使用缓冲流,就相当于 我们在桌面查看一个D盘的文件,每次都得打开D盘然后读取一个字节,然后返回桌面复制到另一文件李敏啊,然后在回到D盘再读取一个字节...这样子。

用了缓冲流呢,每次读取8192个字节,回来复制一下,write(int i)复制时呢就是一个字节一个字节复制的,write(byte[8192],0,len)就是每次复制8192个字节,当然最快。

4.7 缓冲输出流提供方法 flush() 手动刷新数据到目标文件,每次结束之后可以手动调用,方式数据丢失,在关闭流时也会自动调用刷新方法。总结要么关闭也么刷新最好两个都做了。

相关推荐
冷雨夜中漫步4 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴4 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再4 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
绵绵细雨中的乡音4 小时前
深入理解 ET 与 LT 模式及其在 Reactor 模型中的应用
服务器·网络·php
m0_736919106 小时前
C++代码风格检查工具
开发语言·c++·算法
喵手6 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
2501_944934736 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy6 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
黎雁·泠崖7 小时前
【魔法森林冒险】5/14 Allen类(三):任务进度与状态管理
java·开发语言
2301_763472468 小时前
C++20概念(Concepts)入门指南
开发语言·c++·算法