Java的IO流

1 IO 简介

1.1 什么是 IO

对于任何程序设计语言而言,输入输出(Input/Output)系统都是非常核心的功能。程序 运行需要数据,数据的获取往往需要跟外部系统进行通信,外部系统可能是文件、数据库、 其他程序、网络、IO 设备等等。外部系统比较复杂多变,那么我们有必要通过某种手段进行抽象、屏蔽外部的差异,从而实现更加便捷的编程。
输入(Input)指的是:可以让程序从外部系统获得数据(核心含义是“读”,读取外部数据)。 常见的应用:
1.读取硬盘上的文件内容到程序。例如:播放器打开一个视频文件、word 打开一个 doc 文件。
2.读取网络上某个位置内容到程序。例如:浏览器中输入网址后,打开该网址对应的 网页内容;下载网络上某个网址的文件。
3.读取数据库系统的数据到程序。
4.读取某些硬件系统数据到程序。例如:车载电脑读取雷达扫描信息到程序;温控系统等。
输出(Output)指的是:程序输出数据给外部系统从而可以操作外部系统(核心含义是 “写”,将数据写出到外部系统)。常见的应用有:
1.将数据写到硬盘中。例如:我们编辑完一个 word 文档后,将内容写到硬盘上进行保存。
2.将数据写到数据库系统中。例如:我们注册一个网站会员,实际就是后台程序向数 据库中写入一条记录。
3.将数据写到某些硬件系统中。例如:导弹系统导航程序将新的路径输出到飞控子系 统,飞控子系统根据数据修正飞行路径。
将数据写到某些硬件系统中。例如:导弹系统导航程序将新的路径输出到飞控子系 统,飞控子系统根据数据修正飞行路径。

1.2 数据源

数据源 Data Source,提供数据的原始媒介。常见的数据源有:数据库、文件、其他程 序、内存、网络连接、IO设备。
数据源分为:源设备、目标设备。
1.源设备:为程序提供数据,一般对应输入流。
2.目标设备:程序数据的目的地,一般对应输出流。在这里插入图片描述

1.3 流的概念

流是一个抽象、动态的概念,是一连串连续动态的数据集合。
对于输入流而言,数据源就像水箱,流(Stream)就像水管中流动着的水流,程序就是我 们最终的用户。我们通过流(A Stream)将数据源(Source)中的数据(information)输送到程序(Program)中。
对于输出流而言,目标数据源就是目的地(dest),我们通过流(A Stream)将程序 (Program)中的数据(information)输送到目的数据源(dest)中。
在这里插入图片描述

菜鸟雷区:
输入/输出流的划分是相对程序而言的,并不是相对数据源。

1.4 Java 中四大 IO 抽象类

InputStream/OutputStream 和 Reader/Writer 类是所有 IO 流类的抽象父类,我们有必要 简单了解一下这个四个抽象类的作用。然后,通过它们具体的子类熟悉相关的用法。
InputStream
此抽象类是表示字节输入流的所有类的父类。InputSteam 是一个抽象类,它不可以实例 化。 数据的读取需要由它的子类来实现。根据节点的不同,它派生了不同的节点流子类 。
继承自 InputSteam 的流都是用于向程序中输入数据,且数据的单位为字节(8 bit)。
常用方法
int read():读取一个字节的数据,并将字节的值作为 int 类型返回(0-255 之间的一个值)。 如果未读出字节则返回-1(返回值为-1 表示读取结束)。
void close():关闭输入流对象,释放相关系统资源。
OutputStream
此抽象类是表示字节输出流的所有类的父类。输出流接收输出字节并将这些字节发送到 某个目的地。
常用方法
void write(int n):向目的地中写入一个字节。
void close():关闭输出流对象,释放相关系统资源。
Reader
Reader 用于读取的字符流抽象类,数据单位为字符。
int read(): 读取一个字符的数据,并将字符的值作为 int 类型返回(0-65535 之间的一个 值,即 Unicode 值)。如果未读出字符则返回-1(返回值为-1 表示读取结束)。
void close() : 关闭流对象,释放相关系统资源。
Writer
Writer 用于输出的字符流抽象类,数据单位为字符。
void write(int n): 向输出流中写入一个字符。
void close() : 关闭输出流对象,释放相关系统资源。

1.5 Java 中流的概念细分

按流的方向分类:
1.输入流:数据流从数据源到程序(以 InputStream、Reader 结尾的流)。
2.输出流:数据流从程序到目的地(以 OutPutStream、Writer 结尾的流)。 在这里插入图片描述
按处理的数据单元分类:
1.字节流:以字节为单位获取数据,命名上以 Stream 结尾的流一般是字节流, 如 FileInputStream、FileOutputStream。
2.字符流:以字符为单位获取数据,命名上以 Reader/Writer 结尾的流一般是字 符流,如 FileReader、FileWriter。
按处理对象不同分类
1.节点流:可以直接从数据源或目的地读写数据,如 FileInputStream、FileReader、 DataInputStream 等。
2.处理流:不直接连接到数据源或目的地,是”处理流的流”。通过对其他流的处 理提高程序的性能,如 BufferedInputStream、BufferedReader 等。处理流也叫 包装流。
节点流处于 IO 操作的第一线,所有操作必须通过它们进行;处理流可以对节 点流进行包装,提高性能或提高程序的灵活性。在这里插入图片描述

1.6 Java 中 IO 流类的体系

Java 为我们提供了多种多样的 IO 流,我们可以根据不同的功能及性能要求挑选合适的 IO 流。
下图为Java 中 IO 流类的体系(这里只列出常用的类,详情可以参考 JDK API 文档)如图所示:在这里插入图片描述
从上图发现,很多流都是成对出现的,比如:FileInputStream/FileOutputStream,显然 是对文件做输入和输出操作的。我们下面简单做个总结:
1.InputStream/OutputStream 字节流的抽象类。
2.Reader/Writer 字符流的抽象类。
3.FileInputStream/FileOutputStream 节点流:以字节为单位直接操作“文件”。 4
4.ByteArrayInputStream/ByteArrayOutputStream 节点流:以字节为单位直接操作“字节数组对象”。
5.ObjectInputStream/ObjectOutputStream 处理流:以字节为单位直接操作“对象”。
6.DataInputStream/DataOutputStream 处理流:以字节为单位直接操作“基本数据类型与字符串类型”。
7.FileReader/FileWriter 节点流:以字符为单位直接操作“文本文件”(注意:只能读写文本文件)。 8.BufferedReader/BufferedWriter 处理流:将 Reader/Writer 对象进行包装,增加缓存功能,提高读写效率。 9.BufferedInputStream/BufferedOutputStream 处理流:将 InputStream/OutputStream 对象进行包装,增加缓存功能,提高 读写效率。
10.InputStreamReader/OutputStreamWriter 处理流:将字节流对象转化成字符流对象。
11.PrintStream 处理流:将 OutputStream 进行包装,可以方便地输出字符,更加灵活。

建议:
上面的解释,一句话就点中了流的核心作用。大家在后面学习的时候,用心体会。

2 IO 流入门案例

2.1 第一个简单的 IO流程序

当程序需要读取数据源的数据时,就会通过 IO 流对象开启一个通向数据源的流,通过 这个 IO 流对象的相关方法可以顺序读取数据源中的数据。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 通过这个IO流对象的相关方法可以顺序读取数据源中的数据。
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FirstDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            // 创建字节输入流对象
            fis = new FileInputStream("d:/a.txt");
            // 打印输入字符a对应的 ascii码值97
            int s1 = fis.read();
            // 打印输入字符b对应的 ascii码值98
            int s2 = fis.read();
            // 打印输入字符c对应的 ascii码值99
            int s3 = fis.read();
            // 由于文件内容已经读取完毕,则返回-1
            int s4 = fis.read();
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(s4);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

如图所示:在这里插入图片描述

2.2 改造入门案例

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 通过这个IO流对象的相关方法可以顺序读取数据源中的数据。
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class SecondDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            // 创建字节输入流对象
            fis = new FileInputStream("d:/a.txt");
            StringBuilder sb = new StringBuilder();
            int temp = 0;
            while((temp = fis.read()) != -1){
                System.out.println(temp);
                sb.append((char) temp);
            }
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null){
                try {
                    fis.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

如图所示:在这里插入图片描述

3 File类的使用

3.1 File类的作用

File 类是 Java 提供的针对磁盘中的文件或目录转换对象的包装类。一个 File 对象而可以代表一个文件或目录,File 对象可以实现获取文件和目录属性等功能,可以实现对文件和目 录的创建,删除等功能。

3.1.1 File 类操作目录与文件的常用方法

3.1.2 针对文件操作的方法

1.createNewFile()			// 创建新文件。
2.delete()                 // 直接从磁盘上删除
3.exists()				// 查询磁盘中的文件是否存在
4.getAbsolutePath()     // 获取绝对路径 
5.getPath()           // 获取相对路径 
6.getName()         // 获取文件名 相当于调用了一个 toString方法。
7.isFile()			// 判断是否是文件.
8.length()			// 查看文件中的字节数 
9.isHidden()      // 测试文件是否被这个抽象路径名是一个隐藏文件。

3.1.3 针对目录操作的方法

1.exists()		// 查询目录是否存在 
2.isDirectory()			// 判断当前路径是否为目录
3.mkdir()		// 创建目录 
4.getParentFile() 		// 获取当前目录的父级目录。 l
5.ist()			// 返回一个字符串数组,包含目录中的文件和目录的路径名。
6.listFiles   // 返回一个File数组,表示用此抽象路径名表示的目录中的文件。

3.2 File类的基本使用

3.2.1 操作文件

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件操作
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileDemo {
    public static void main(String[] args) throws Exception {
        // 创建File对象
        File file = new File("d:/aa.txt");
        System.out.println(file.createNewFile());
//        System.out.println(file.delete());
        System.out.println(file.exists());
        System.out.println(file.getName()); 
        System.out.println(file.isFile()); 
        System.out.println(file.isHidden());
    }
}

如图所示:在这里插入图片描述

3.2.2操作目录

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 操作目录
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class DirectoryDemo {
    public static void main(String[] args) {
        // 创建File对象
        File file = new File("d:/b/c");
       //System.out.println(file.mkdir());
       //System.out.println(file.mkdirs());
        // System.out.println(file.exists());
        // System.out.println(file.isDirectory());
        // System.out.println(file.getParent());
        // System.out.println(file.getParentFile().getName());
        File file2 = new File("d:/");
        String[] arr = file2.list();
        for (String temp: arr){
            System.out.println(temp);
        }
        System.out.println("-----------------------");
        File[] arr2 = file2.listFiles();
        for (File temp: arr2){
            System.out.println(temp);
        }
    }
}

如图所示:在这里插入图片描述

4 常用流对象

4.1 文件字节流

FileInputStream 通过字节的方式读取文件,适合读取所有类型的文件(图像、视频、文 本文件等)。Java 也提供了 FileReader 专门读取文本文件。
FileOutputStream 通过字节的方式写数据到文件中,适合所有类型的文件。Java 也提供 了 FileWriter 专门写入文本文件。

4.1.1文件字节输入流

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件字节输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        try {
            // 创建字节输入流对象
            fis = new FileInputStream("d:/Adair.jpg");
            int temp = 0;
            while((temp = fis.read()) != -1){
                System.out.println(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null){
                    fis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.1.2 文件字节输出流

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件字节输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileStreamDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            // 创建文件字节输入流对象
            fis = new FileInputStream("d:/Adair.jpg");
            // 创建文件字节输出流对象
            fos = new FileOutputStream("d:/aa.jpg");
            int temp = 0;
            while((temp = fis.read()) != -1){
                fos.write(temp);
            }
            // 将数据从内存中写入到磁盘中。
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null){
                    fis.close();
                }
                if (fos != null){
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.1.3通过缓冲区提高读写效率

4.1.3.1 方式一

通过创建一个指定长度的字节数组作为缓冲区,以此来提高 IO 流的读写效率。该方式 适用于读取较大图片时的缓冲区定义。注意:缓冲区的长度一定是 2 的整数幂。一般情况下 1024 长度较为合适。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件字节输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileStreamBuffedDemo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("d:/Adair.jpg");
            // 创建文件字节输出流对象
            fos = new FileOutputStream("d:/aa.jpg");
            // 创建一个缓冲区,提高读写效率
            byte[] buff = new byte[1024];
            int temp = 0;
            while((temp = fis.read(buff)) != -1){
                fos.write(buff,0,temp);
            }
            // 将数据从内存中写入到磁盘中。
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null){
                    fis.close();
                }
                if (fos != null){
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.1.3.2 方式二

通过创建一个字节数组作为缓冲区,数组长度是通过输入流对象的 available()返回当前 文件的预估长度来定义的。在读写文件时,是在一次读写操作中完成文件读写操作的。注意: 如果文件过大,那么对内存的占用也是比较大的。所以大文件不建议使用该方法。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件字节输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileStreamBuffer2Demo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            // 创建文件字节输入流对象
            fis = new FileInputStream("d:/Adair.jpg");
            // 创建文件字节输出流对象
            fos = new FileOutputStream("d:/aa.jpg");
            // 创建一个缓冲区,提高读写效率
            byte[] buff = new byte[fis.available()];
            fis.read(buff);
            // 将数据从内存中写入到磁盘中。
            fos.write(buff);
            fos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null){
                    fis.close();
                }
                if (fos != null){
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.1.4 通过字节缓冲流提高读写效率

Java 缓冲流本身并不具有 IO 流的读取与写入功能,只是在别的流(节点流或其他处理 流)上加上缓冲功能提高效率,就像是把别的流包装起来一样,因此缓冲流是一种处理流(包 装流)。
当对文件或者其他数据源进行频繁的读写操作时,效率比较低,这时如果使用缓冲流就 能够更高效的读写信息。因为缓冲流是先将数据缓存起来,然后当缓存区存满后或者手动刷 新时再一次性的读取到程序或写入目的地。
因此,缓冲流还是很重要的,我们在 IO 操作时记得加上缓冲流来提升性能。
BufferedInputStream 和 BufferedOutputStream 这两个流是缓冲字节流,通过内部缓存数 组来提高操作流的效率。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件字节输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileStreamBuffed3Demo {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream("d:/Adair.jpg");
            bis = new BufferedInputStream(fis);
            fos = new FileOutputStream("d:/aa.jpg");
            bos = new BufferedOutputStream(fos);
            // 缓冲流中的byte数组长度默认是8192
            int temp = 0;
            while((temp = bis.read()) != -1){
                bos.write(temp);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //  注意:关闭流顺序:"后开的先关闭"
                if (bis != null) {
                    bis.close();
                }
                if (fis != null){
                    fis.close();
                }
                if (bos != null){
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.1.5 定义文件拷贝工具类

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 定义文件拷贝工具类
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileCopyTools {
    public static void main(String[] args) {
        copyFile("d:/Adair.jpg","d:/aa.jpg");
    }

    /**
     * 文件拷贝方法
     * @param src
     * @param des
     */
    public static void copyFile(String src,String des){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(src));
            bos = new BufferedOutputStream(new FileOutputStream(des));
            int temp = 0;
            while((temp = bis.read()) != -1){
                bos.write(temp);
            }
            bos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                //  注意:关闭流顺序:"后开的先关闭"
                if (bis != null) {
                    bis.close();
                }
                if (fis != null){
                    fis.close();
                }
                if (bos != null){
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.2 文件字符流

前面介绍的文件字节流可以处理所有的文件,如果我们处理的是文本文件,也可以使用 文件字符流,它以字符为单位进行操作。

4.2.1 文件字符输入流

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件字符输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileReaderDemo {
    public static void main(String[] args) {
        FileReader frd = null;
        try {
            // 创建文件字符输入流对象
            frd = new FileReader("d:/a.txt");
            int temp = 0;
            while ((temp = frd.read()) != -1 ){
                System.out.println((char) temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (frd != null){
                    frd.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.2.2 文件字符输出流

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 文件字符输出流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileWriterDemo {
    public static void main(String[] args) {
        FileWriter fw = null;
        FileWriter fw2 = null;
        try {
            // 创建字符输出流对象
            fw = new FileWriter("d:/a.txt");
            fw.write("你好学无止路\r\n");
            fw.write("你好Adair\r\n");
            fw.flush();
            fw2 = new FileWriter("d:/a.txt",true);
            fw2.write("未来自己\r\n相信学无");
            fw2.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw != null) {
                    fw.close();
                }
                if (fw2 != null) {
                    fw2.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.2.3 使用字符流实现文本文件的拷贝处理

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 使用字符流实现文本文件的拷贝处理
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileCopyTools2 {
    public static void main(String[] args) {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("d:/2.txt");
            fw = new FileWriter("d:/3.txt");
            char[] buff = new char[1024];
            int temp = 0;
            while ((temp = fr.read(buff)) != -1){
                fw.write(buff,0,temp);
            }
            fw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(fr != null){
                    fr.close();
                }
                if (fw != null){
                    fw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.3 字符缓冲流

BufferedReader/BufferedWriter 增加了缓存机制,大大提高了读写文本文件的效率。

4.3.1 字符输入缓冲流

BufferedReader是针对字符输入流的缓冲流对象,提供了更方便的按行读取的方法: readLine(); 在使用字符流读取文本文件时,我们可以使用该方法以行为单位进行读取。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 字符输入缓冲流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class BufferedReaderDemo {
    public static void main(String[] args) {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("d:/a.txt");
            br = new BufferedReader(fr);
            String temp = "";
            while ((temp = br.readLine()) != null){
                System.out.println(temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(br != null){
                    br.close();
                }
                if(fr != null){
                    fr.close();
                }
            }catch(Exception e){
            	e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.3.2字符输出缓冲流

BufferedWriter 是针对字符输出流的缓冲流对象,在字符输出缓冲流中可以使用 newLine();方法实现换行处理。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 字符输出缓冲流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class BufferedWriterDemo {
    public static void main(String[] args) {
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter("d:/a.txt");
            bw = new BufferedWriter(fw);
            bw.write("你好学无止路");
            bw.newLine();
            bw.write("你好Adair");
            bw.newLine();
            bw.write("未来自己");
            bw.newLine();
            bw.write("相信学无");
            bw.newLine();
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(bw != null){
                    bw.close();
                }
                if(fw != null){
                    fw.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.3.3 通过字符缓冲流实现文本文件的拷贝

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 通过字符缓冲流实现文本文件的拷贝
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileCopyTools3 {
    public static void main(String[] args) {
        copyFile("d:/2.txt","d:/22.txt");
    }

    /**
     * 基于字符流缓冲流实现文件拷贝
     * @param src
     * @param des
     */
    public static void copyFile(String src,String des){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(src));
            bw = new BufferedWriter(new FileWriter(des));
            String temp = "";
            while ((temp = br.readLine()) != null){
                bw.write(temp);
                bw.newLine();
            }
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.3.4 通过字符缓冲流为文件中的内容添加行号

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 通过字符缓冲流为文件中的内容添加行号
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class LineNumberDemo {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader("d:/a.txt"));
            bw = new BufferedWriter(new FileWriter("d:/d.txt"));
            String temp = "";
            int i = 1;
            while ((temp = br.readLine()) != null){
                bw.write(i + "," + temp);
                bw.newLine();
                i ++;
            }
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.4 转换流

InputStreamReader/OutputStreamWriter 用来实现将字节流转化成字符流。比如,如下场景:
System.in 是字节流对象,代表键盘的输入,如果我们想按行接收用户的输入时,就必 须用到缓冲字符流 BufferedReader 特有的方法 readLine(),但是经过观察会发现在创建 BufferedReader 的 构 造 方 法 的 参 数 必 须 是 一 个 Reader 对 象 , 这 时 候 我 们 的 转 换 流 InputStreamReader 就派上用场了。
而 System.out 也是字节流对象,代表输出到显示器,按行读取用户的输入后,并且要将读取的一行字符串直接显示到控制台,就需要用到字符流的 write(String str)方法,所以我们 要使用 OutputStreamWriter 将字节流转化为字符流。

4.4.1 通过转换流实现键盘输入屏幕输出

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 通过转换流实现键盘输入屏幕输出
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class ConvertStream {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(System.in));
            bw = new BufferedWriter(new OutputStreamWriter(System.out));
            while (true){
                bw.write("请输入:");
                bw.flush();
                String input = br.readLine();
                if ("exit".equals(input)) {
                    break;
                }
                bw.write("你输入的是:" + input);
                bw.newLine();
                bw.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.4.2 通过字节流读取文本文件并添加行号

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 通过字节流读取文本文件并添加行号
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class LineNumberDemo2 {
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/a.txt")));
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:/d.txt")));
            String temp = "";
            int i = 1;
            while ((temp = br.readLine()) != null){
                bw.write(i + "." + temp);
                bw.newLine();
                i ++;
            }
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.5 字符输出流

在Java 的 IO 流中专门提供了用于字符输出的流对象 PrintWriter。该对象具有自动行刷 新缓冲字符输出流,特点是可以按行写出字符串,并且可通过 println();方法实现自动换行。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 字符输出流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class LineNumberDemo3 {
    public static void main(String[] args) {
        BufferedReader br = null;
        PrintWriter pw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/a.txt")));
            pw = new PrintWriter("d:/d.txt");
            String temp = "";
            int i = 1;
            while ((temp = br.readLine()) != null){
                pw.println(i + "." + temp);
                i ++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (pw != null) {
                    pw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.6 字节数组流

ByteArrayInputStream 和 ByteArrayOutputStream 经常用在需要流和数组之间转化的情况!

4.6.1 字节数组输入流

说白了,FileInputStream 是把文件当做数据源。ByteArrayInputStream 则是把内存中的” 字节数组对象”当做数据源。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 字节数组输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class ByteArrayInputDemo {
    public static void main(String[] args) {
        byte[] arr = "abcdefg".getBytes();
        ByteArrayInputStream bis = null;
        StringBuilder sb = new StringBuilder();
        try {
            // 该构造方法的参数是一个字节数组,这个字节数组就是数据源
            bis = new ByteArrayInputStream(arr);
            int temp = 0;
            while ((temp = bis.read()) != -1){
                sb.append((char)temp);
            }
            System.out.println(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.6.2 字节数组输出流

ByteArrayOutputStream 流对象是将流中的数据写入到字节数组中。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 字节数组输出流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class ByteArrayOutputDemo {
    public static void main(String[] args) {
        ByteArrayOutputStream bos = null;
        try {
            StringBuilder sb = new StringBuilder();
            bos = new ByteArrayOutputStream();
            bos.write('a');
            bos.write('b');
            bos.write('c');
            byte[] arr = bos.toByteArray();
            for(int i = 0;i < arr.length;i ++){
                sb.append((char)arr[i]);
            }
            System.out.println(sb.toString());
        } finally {
            try {
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.7 数据流

数据流将“基本数据类型与字符串类型”作为数据源,从而允许程序以与机器无关的方式 从底层输入输出流中操作 Java 基本数据类型与字符串类型。
DataInputStream 和 DataOutputStream 提供了可以存取与机器无关的所有 Java 基础类型 数据(如:int、double、String 等)的方法。

4.7.1 数据输出流

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 数据输出流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class DataOutputDemo {
    public static void main(String[] args) {
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("d:/d.txt")));
            dos.writeChar('a');
            dos.writeInt(10);
             dos.writeDouble(Math.random()); 
             dos.writeBoolean(true);
            dos.writeUTF("你好学无止路");
            dos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

4.7.2 数据输入流

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 数据输入流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class DataInputDemo {
    public static void main(String[] args) {
        DataInputStream dis = null;
        try {
            dis = new DataInputStream(new BufferedInputStream(new FileInputStream("d:/d.txt")));
            // 直接读取数据,注意:读取的顺序要与写入的顺序一致,否则不能正确读 取数据。
            System.out.println("char: " + dis.readChar());
            System.out.println("int: " + dis.readInt());
            System.out.println("double: " + dis.readDouble());
            System.out.println("boolean: " + dis.readBoolean());
            System.out.println("String: " + dis.readUTF());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (dis != null) {
                    dis.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

5 对象流

对象的本质是用来组织和存储数据的,对象本身也是数据。那么,能不能将对象存储到 硬盘上的文件中呢?能不能将对象通过网络传输到另一个电脑呢?我们可以通过序列化和 反序列化来实现这些需求。

5.1 Java 对象的序列化和反序列化

5.1.1 序列化和反序列化是什么

当两个进程远程通信时,彼此可以发送各种类型的数据。 无论是何种类型的数据,都 会以二进制序列的形式在网络上传送。比如,我们可以通过 http 协议发送字符串信息;我 们也可以在网络上直接发送 Java 对象。发送方需要把这个 Java 对象转换为字节序列,才能 在网络上传送;接收方则需要把字节序列再恢复为 Java 对象才能正常读取。
把 Java 对象转换为字节序列的过程称为对象的序列化。把字节序列恢复为 Java 对象的 过程称为对象的反序列化
对象序列化的作用有如下两种:
1.持久化: 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中。
2.网络通信:在网络上传送对象的字节序列。比如:服务器之间的数据通信、对象传 递。

5.1.2 序列化涉及的类和接口

ObjectOutputStream 代表对象输出流,它的 writeObject(Object obj)方法可对参数指定的 obj 对象进行序列化,把得到的字节序列写到一个目标输出流中。
ObjectInputStream 代表对象输入流,它的 readObject()方法从一个源输入流中读取字节 序列,再把它们反序列化为一个对象,并将其返回。
只有实现了 Serializable 接口的类的对象才能被序列化。Serializable 接口是一个空接口, 只起到标记作用。

5.2 操作基本数据类型

我们前边学到的数据流只能实现对基本数据类型和字符串类型的读写,并不能对 Java 对象进行读写操作(字符串除外),但是在对象流中除了能实现对基本数据类型进行读写操 作以外,还可以对 Java 对象进行读写操作。

5.2.1 写出基本数据类型数据

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 写出基本数据类型数据
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class ObjectOutputStreamBasicTypeDemo {
    public static void main(String[] args) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/a.txt")));
            oos.writeInt(10);
            oos.writeDouble(Math.random());
            oos.writeChar('a');
            oos.writeBoolean(true);
            oos.writeUTF("你好 Adair");
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(oos != null){
                    oos.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

5.2.2 读取基本数据类型数据

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 读取基本数据类型数据
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class ObjectInputStreamBasicTypeDemo {
    public static void main(String[] args) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("d:/a.txt")));
            // 必须要按照写入的顺序读取数据
            System.out.println("int: " + ois.readInt());
            System.out.println("double: " + ois.readDouble());
            System.out.println("char: " + ois.readChar());
            System.out.println("boolean: " + ois.readBoolean());
            System.out.println("String: " + ois.readUTF());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(ois != null){
                    ois.close();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

5.3 操作对象

5.3.1 将对象序列化到文件

ObjectOutputStream 可以将一个内存中的 Java 对象通过序列化的方式写入到磁盘的文 件中。被序列化的对象必须要实现 Serializable 序列化接口,否则会抛出异常。

5.3.1.1 创建对象

演示的代码如下:

package com.txw;

import java.io.Serializable;
/**
 * 用户
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class Users implements Serializable {
    private int id;
    private String name;
    private int age;

    public Users() {
    }

    public Users(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

如图所示:在这里插入图片描述

5.3.1.2 序列化对象

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 序列化对象
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class ObjectOutputStreamObjectTypeDemo {
    public static void main(String[] args) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("d:/d.txt")));
            Users users = new Users(1,"Adair",25);
            oos.writeObject(users); oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

5.3.2 将对象反序列化到内存中

演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 将对象反序列化到内存中
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class ObjectInputStreamObjectTypeDemo {
    public static void main(String[] args) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("d:/d.txt")));
            Users users = (Users) ois.readObject();
            System.out.println(users.getId() + "\t" + users.getName() + "\t" + users.getAge());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

6 随机访问流

RandomAccessFile 可以实现两个作用:
1.实现对一个文件做读和写的操作。
2.可以访问文件的任意位置。不像其他流只能按照先后顺序读取。
在开发某些客户端软件时,经常用到这个功能强大的可以”任意操作文件内容”的类。比 如,软件的使用次数和使用日期,可以通过本类访问文件中保存次数和日期的地方进行比对 和修改。 Java 很少开发客户端软件,所以在 Java 开发中这个类用的相对较少。
学习这个流我们需掌握三个核心方法:
1.RandomAccessFile(String name, String mode) name 用来确定文件; mode 取 r(读)或 rw(可读写),通过 mode 可以确定流对文件的访问权限。
2.seek(long a) 用来定位流对象读写文件的位置,a 确定读写位置距离文件开头的字节个数。
3.getFilePointer() 获得流的当前读写位置。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * 随机访问流
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class RandomAccessFileDemo {
    public static void main(String[] args) {
        RandomAccessFile raf = null;
        try {
            raf = new RandomAccessFile("d:/d.txt","rw");
            // 将若干数据写入到文件中
            int[] arr = new int[]{10,20,30,40,50,60,70,80,90,100};
            for (int i = 0; i < arr.length; i++) {
                raf.writeInt(arr[i]);
            }
            raf.seek(4);
            System.out.println(raf.readInt());
            // 隔一个读一个数据
            for(int i = 0;i < 10; i += 2){
                raf.seek(i * 4);
                System.out.print(raf.readInt() + "\t");
            }
            System.out.println();
            // 在第8个字节位置插入一个新的数据45,替换之前的数据30
            raf.seek(8);
            raf.writeInt(45); 
            for(int i = 0;i < 10; i += 2){
                raf.seek(i * 4);
                System.out.print(raf.readInt() + "\t");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (raf != null) {
                    raf.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

7 File类在IO中的作用

当以文件作为数据源或目标时,除了可以使用字符串作为文件以及位置的指定以外,我 们也可以使用 File 类指定。
演示的代码如下:

package com.txw;

import java.io.*;
/**
 * File类在IO中的作用
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileInIODemo{
    public static void main(String[] args) {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader("d:/a.txt"));
            bw = new BufferedWriter(new FileWriter("d:/d.txt"));
            String temp = "";
            int i = 1;
            while((temp = br.readLine()) != null){
                bw.write(i + "," + temp);
                bw.newLine();
                i ++;
            }
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (bw != null) {
                    bw.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

如图所示:在这里插入图片描述

8 Apache IO 包

JDK 中提供的文件操作相关的类,但是功能都非常基础,进行复杂操作时需要做大量编 程工作。实际开发中,往往需要你自己动手编写相关的代码,尤其在遍历目录文件时,经常 用到递归,非常繁琐。 Apache-commons 工具包中提供了 IOUtils/FileUtils,可以让我们非常 方便的对文件和目录进行操作。 本文就是让大家对 IOUtils/FileUtils 类有一个全面的认识, 便于大家以后开发与文件和目录相关的功能。
Apache IOUtils 和 FileUtils 类库为我们提供了更加简单、功能更加强大的文件操作和 IO 流操作功能。非常值得大家学习和使用。

8.1 Apache 基金会介绍

Apache 软件基金会(也就是 Apache Software Foundation,简称为 ASF),是专门为支持 开源软件项目而办的一个非盈利性组织。在它所支持的 Apache 项目与子项目中,所发行的 软件产品都遵循 Apache 许可证(Apache License)。 官方网址为:www.apache.org 。
很多著名的 Java 开源项目都来源于这个组织。比如:commons、kafka、lucene、maven、 shiro、struts 等技术,以及大数据技术中的:hadoop(大数据第一技术)、hbase、spark、 storm、mahout 等。

8.2 下载与添加commons-io包

8.2.1 下载地址

https://commons.apache.org/proper/commons-io/download_io.cgi

如图所示:在这里插入图片描述

8.2.2 添加 jar 包

1.点击File,选择Project Structure。如图所示:在这里插入图片描述
2.选择Libraries,点击+,选择JAVA。如图所示:在这里插入图片描述
3.如图所示:点击OK即可。在这里插入图片描述
4.如图所示,说明添加成功!在这里插入图片描述

8.3 FileUtils的使用

FileUtils 类中常用方法: 打开 FileUtils 的 api 文档,我们抽出一些工作中比较常用的方法,进行总结和讲解。总结如下:

cleanDirectory:清空目录,但不删除目录。
contentEquals:比较两个文件的内容是否相同。
copyDirectory:将一个目录内容拷贝到另一个目录。可以通过FileFilter过滤需要拷贝的 文件。 
copyFile:将一个文件拷贝到一个新的地址。 
copyFileToDirectory:将一个文件拷贝到某个目录下。 
copyInputStreamToFile:将一个输入流中的内容拷贝到某个文件。
deleteDirectory:删除目录。 
deleteQuietly:删除文件。 
listFiles:列出指定目录下的所有文件。
openInputSteam:打开指定文件的输入流。 
readFileToString:将文件内容作为字符串返回。
readLines:将文件内容按行返回到一个字符串数组中。 
size:返回文件或目录的大小。 
write:将字符串内容直接写到文件中。 
writeByteArrayToFile:将字节数组内容写到文件中。 
writeLines:将容器中的元素的 
toString 方法返回的内容依次写入文件中。
writeStringToFile:将字符串内容写到文件中。

8.3.1 FileUtils的使用一

演示的代码如下:

package com.txw;

import org.apache.commons.io.FileUtils;
import java.io.File;
/**
 * FileUtils的使用一
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileUtilsDemo1 {
    public static void main(String[] args) throws Exception {
        String content = FileUtils.readFileToString(new File("d:/a.txt"),"utf-8");
        System.out.println(content);
    }
}

如图所示:在这里插入图片描述

8.3.2 FileUtils的使用二

演示的代码如下:

package com.txw;

import org.apache.commons.io.FileUtils;
import java.io.*;
/**
 * FileUtils的使用二
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class FileUtilsDemo2 {
    public static void main(String[] args) throws Exception{
        FileUtils.copyDirectory(new File("d:/a"), new File("c:/a"), new FileFilter() {
            /**
             * 在文件拷贝时的过滤条件
             * @param pathname
             * @return
             */
            @Override
            public boolean accept(File pathname) {
                if(pathname.isDirectory() || pathname.getName().endsWith("html")){
                    return true;
                }return false;
            }
        });
    }
}

如图所示:在这里插入图片描述

8.4 4 IOUtils的使用

打开IOUtils 的api文档,我们发现它的方法大部分都是重载的。所以,我们理解它的方 法并不是难事。因此,对于方法的用法总结如下:

buffer 方法:将传入的流进行包装,变成缓冲流。并可以通过参数指定缓冲大小。
closeQueitly 方法:关闭流。
contentEquals 方法:比较两个流中的内容是否一致。
copy 方法:将输入流中的内容拷贝到输出流中,并可以指定字符编码。
copyLarge 方法:将输入流中的内容拷贝到输出流中,适合大于 2G 内容的拷贝。
lineIterator 方法:返回可以迭代每一行内容的迭代器。
read 方法:将输入流中的部分内容读入到字节数组中。
readFully 方法:将输入流中的所有内容读入到字节数组中。
readLine 方法:读入输入流内容中的一行。
toBufferedInputStream,toBufferedReader:将输入转为带缓存的输入流。
toByteArray,toCharArray:将输入流的内容转为字节数组、字符数组。
toString:将输入流或数组中的内容转化为字符串。
write 方法:向流里面写入内容。
writeLine 方法:向流里面写入一行内容。
演示的代码如下:

package com.txw;

import org.apache.commons.io.IOUtils;
import java.io.*;
/**
 * IOUtils的使用
 * @author Adair
 * @email 1578533828@qq.com
 */
@SuppressWarnings("all")  // 注解警告信息
public class IOUtilsDemo {
    public static void main(String[] args) throws Exception {
        String content = IOUtils.toString(new FileInputStream("d:/d.txt"),"utf-8");
        System.out.println(content);
    }
}

如图所示:在这里插入图片描述

9 总结

1.按流的方向分类:
1.1 输入流:数据源到程序(InputStream、Reader 读进来)。
1.2 输出流:程序到目的地(OutPutStream、Writer 写出去)。
2.按流的处理数据单元分类:
2.1 字节流:按照字节读取数据(InputStream、OutputStream)。
2.2 字符流:按照字符读取数据(Reader、Writer)。
3.按流的功能分类:
3.1 节点流:可以直接从数据源或目的地读写数据。
3.2 处理流:不直接连接到数据源或目的地,是处理流的流。通过对其他流的处理 提高程序的性能。
4.IO 的四个基本抽象类:InputStream、OutputStream、Reader、Writer。
5.InputStream 的实现类:
5.1 FileInputStream
5.2 ByteArrayInutStream
5.3 BufferedInputStream
5.4 DataInputStream
5.5 ObjectInputStream
6.OutputStream 的实现类:
6.1 FileOutputStream
6.2 ByteArrayOutputStream
6.3 BufferedOutputStream
6.4 DataOutputStream
6.5 ObjectOutputStream
6.6 PrintStream
7.Reader 的实现类
7.1 FileReader
7.2 BufferedReader
7.3 InputStreamReader
8.Writer 的实现类
8.1 FileWriter
8.2 BufferedWriter
8.3 OutputStreamWriter
9.把Java对象转换为字节序列的过程称为对象的序列化。
10把字节序列恢复为Java对象的过程称为对象的反序列化。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学无止路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值