小白都能吃透Java IOl流,最骚最全笔记,没有之一!

895 阅读13分钟

「本文已参与好文召集令活动,点击查看:后端、大前端双赛道投稿,2万元奖池等你挑战!

一、File类

​ File课理解为文件和文件夹(目录),用于表示磁盘中某个文件或文件夹的路径。该类包含了文件的创建、删除、重命名、判断是否存在等方法。

​ 只能设置和获取文件本身的信息(文件大小,是否可读),不能设置和获取文件里面的内容。

​ 不同的操作系统对盘符的符号的定义是不同的:

  1. Unix: 严格区分大小写,使用”/”来表示路径分隔符。
  2. Windows: 默认情况下是不区分大小写的,使用”\”来分割目录路径。但是在Java中一个”\”表示转义,所以在Windows系统中就得使用两个”\\”。

1.1、File常用方法

  • String getName():获取文件名称

  • String getPath():获取文件路径

  • String getAbsolutePath():获取绝对路径

  • File getParentFile():获取上级目录文件

  • boolean exists():判断文件是否存在

  • boolean isFile() :是否是文件

  • boolean isDirectory():判断是否是目录

  • boolean delete() :删除文件

  • boolean mkdirs():创建当前目录和上级目录

  • File[] listFiles() :列出所有文件对象

public class FileDemo {
	public static void main(String[] args) throws Exception {
		File f = new File("C:/test/123.txt");
		System.out.println(f.getName());//123.txt
		System.out.println(f.getPath());//C:/test/123.txt
		System.out.println(f.getAbsolutePath());//C:/test/123.txt
		System.out.println(f.getParentFile().getName());//test
		System.out.println(f.exists());//true
		System.out.println(f.isFile());//true
		System.out.println(f.isDirectory());//false
		
		//如果当前文件的父文件夹不存在,则创建
		if(!f.getParentFile().exists()) {
			f.getParentFile().mkdirs();
		}
		//列出当前文件夹中所有文件
		File[] fs = f.getParentFile().listFiles();
		for (File file : fs) {
			System.out.println(file);
		}
	}
}

1.2、练习

​ 列出给定目录中的全部文件的路径,包括给定目录下面的所有子目录。(递归)

public static void list(File file) {
	if (file.isDirectory()) {
		// 如果是文件夹,则继续列出
		File[] fs = file.listFiles();//列出所有文件
		if (fs != null) {//如果文件不为空,说明里面还有文件夹
			for (File f : fs) {//遍历文件夹
				list(f);//递归调用
			}
		}
	}
	System.out.println(file);//输出文件
}

二、IO流操作

​ 什么是IO,Input和Output,即输入和输出。IO就是一种流。

2.1、流的分类

​ 根据流的不同特性,流的划分是不一样的,一般按照如下情况来考虑:

  • 按流动方向:分为输入流和输出流

  • 按数据传输单位:分为字节流和字符流,即每次传递一个字节(byte)或一个字符(char)

  • 按功能上划分:分为节点流和处理流,节点流功能单一,处理流功能更强

流的流向是相对的,我们一般站在程序的角度:

  • 程序需要数据 → 把数据读进来 → 输入操作(read):读进来

  • 程序保存数据 → 把数据写出去 → 输出操作(write):写出去

    简而言之:读进来,写出去

2.2、四大基本流

流向字节流(单位是字节)字符流(单位是字符)
输入流InputStreamReader
输出流OutputStreamWriter

2.3、操作IO流的模板

1):创建源或者目标对象(挖井).

   输入操作:   把文件中的数据流向到程序中,此时文件是源,程序是目标.

   输出操作:   把程序中的数据流向到文件中,此时文件是目标,程序是源.

2):创建IO流对象(水管).

   输入操作:   创建输入流对象.

   输出操作:   创建输出流对象.

3):具体的IO操作.

   输入操作:   输入流对象的read方法.

   输出操作:   输出流对象的write方法.

4):关闭资源(勿忘). 一旦资源关闭之后,就不能使用流对象了,否则报错.

   输入操作:   输入流对象.close();

   输出操作:   输出流对象.close();

注意:

  1. 四大抽象流是不能创建对象的,我们会根据需求不同去选择他们的子类,比如操作文件的时候就选择文件流
  2. 无论是什么流,到最后都要调用close方法关闭流,释放资源

三、字节输入流(InputStream)

public abstract class InputStream extends Object implements Closeable,表示字节输入流的所有类的超类。

3.1、常用方法

  • public void close() throws IOException:关闭此输入流并释放与该流关联的所有系统资源。 InputStream 的 close 方法不执行任何操作。

  • public abstract int read() throws IOException:从输入流中读取一个字节数据并返回该字节数据,如果到达流的末尾,则返回 -1。

  • public int read(byte[] buff) throws IOException:从输入流中读取多个字节数据,并存储在缓冲区数组 buff 中。返回已读取的字节数量,如果已到达流的末尾,则返回 -1。

package day17_IO.classing;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;

/**
 * @author Xiao_Lin
 * @date 2020/12/22 19:48
 */
public class InputStream {

  public static void main(String[] args) throws IOException {
    public static void main(String[] args) throws IOException {
        // 准备File对象  找绝对路径
        File file = new File("123.txt");
        // 编译  运行的是字节码文件
        System.out.println("file.getAbsolutePath() = " + file.getAbsolutePath());
        // 创建输入流
        FileInputStream inputStream = new FileInputStream(file);
        // 方式一:  不可取
        //当inputStream.read()没有传入参数的时候,返回的是当前读到的值
//        int i = inputStream.read(); // A
//        inputStream.read(); // B
//        inputStream.read(); // C
//        inputStream.read(); // D
//        System.out.println("inputStream.read() = " + inputStream.read());

        // 方式二: 一般般  没有缓存
        int i ;
        while ((i=inputStream.read())!=-1){
            System.out.println("i = " + (char)i);
        }

        // 方式三:  一般般 有缓存,但是缓存需要提前定义好大小 ,可能会浪费

//        byte[] b = new byte[1024];
//        while (inputStream.read(b)!=-1) {
//            System.out.println("new String(b) = " + new String(b));
//        }

        // 方式四: 有缓存 用多少缓存,就占用多少
        byte[] b = new byte[1024];
        int off = 0;
        int len ;
        //当len=inputStream.read(b)中传递了一个数组的时候,返回的是这个数组的长度
        while ((len=inputStream.read(b)) !=-1){
                System.out.println("len = " + len);
                System.out.println("new String(b) = " + new String(b,off,len));
            }

        // 关闭资源
        inputStream.close();
    }
}

四、字节输出流(OutputStream)

public abstract class OutputStream extends Object implements Closeable, Flushable表示字节输出流的所有类的超类。

4.1、常用方法

  • public void close() throws IOException:关闭此输出流并释放与此流有关的所有系统资源。

  • public abstract void write(int b) throws IOException:将指定的一个字节数据b写入到输出流中。

  • public void write(byte[] buff) throws IOException:把数组buff中所有字节数据写入到输出流中。

  • public void write(byte[] b, int off,int len) throws IOException:把数组buff中从索引off 开始的len 个字节写入此输出流中。

package day17_IO.classing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Xiao_Lin
 * @date 2020/12/22 20:09
 */
public class OutputStream {

  public static void main(String[] args) throws IOException {
   // 准备File对象  找绝对路径
        File file = new File("123.txt");
        // 创建输出流对象
        FileOutputStream outputStream = new FileOutputStream(file);
        // 调用方法往里面写数据  默认false覆盖
        outputStream.write("ABC".getBytes());
        // 刷新缓存
        outputStream.flush();
        // 关闭资源
        outputStream.close();
  }
}

五、字符输入流(Reader)

public abstract class Reader extends Object implements Readable, Closeable表示字符输入流的所有类的超类。

5.1、常用方法

常用方法:

  • public abstract void close() throws IOException:关闭此输入流并释放与该流关联的所有系统资源。

  • public int read() throws IOException:从输入流中读取一个字符数据并返回该字符数据,如果到达流的末尾,则返回 -1。

  • public int read(char[] cbuf) throws IOException:从输入流中读取多个字符,并存储在缓冲区数组 cbuf 中。返回已读取的字符数,如果已到达流的末尾,则返回 -1。

package day17_IO.classing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

/**
 * @author Xiao_Lin
 * @date 2020/12/22 20:15
 */
public class ReaderDemo {

  public static void main(String[] args) throws IOException {
       // 准备File对象  找绝对路径
        File file = new File("123.txt");
        // 创建FileReader
        FileReader reader = new FileReader(file);
//        reader.read(); // A
//        reader.read(); // B
//        reader.read(); // C
//        int read = reader.read();
//        System.out.println("read = " + read);


        char[] bufer = new char[1024];
        //reader.read(bufer); // 读完了 流中没有数据了,数据都在bufer
        int len = -1;
        while ((len=reader.read(bufer)) !=-1){
            String s = new String(bufer,0,len);
            System.out.println("s = " + s);
        }
        // 关闭资源
        reader.close();
    }

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>(){
            {
                this.put("a","a");
                this.put("b","b");
                this.put("c","c");

            }
        };

        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            //Map.Entry<String, String> next = iterator.next();
           // System.out.println("next = " + next);
            if (iterator.next().getKey().equals("a")) {
                System.out.println("===============");
                iterator.remove();
            }
        }
        System.out.println( map);
  }
}

六、字符输出流(Writer)

public abstract class Writer extends Object implements Appendable, Closeable, Flushable表示字符输出流的所有类的超类。

6.1、常用方法

常用方法:

  • public abstract void flush() throws IOException:刷新此输出流并强制写出所有缓冲的输出字符。

  • public abstract void close() throws IOException:关闭此输入流并释放与该流关联的所有系统资源。

  • public void write(int c) throws IOException:将指定的一个字符数据c写入到输出流中。

  • public void write(char[] cbuf) throws IOException:把数组cbuf中cbuf.length 个字符数据写入到输出流中。

  • public abstract void write(char[] cbuf, int off,int len) throws IOException:把数组cbuf中从索引off 开始的len 个字符写入此输出流中。

  • public void write(String str) throws IOException:将str字符串数据写入到输出流中。

package day17_IO.classing;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Xiao_Lin
 * @date 2020/12/22 20:44
 */
public class WriterDemo {

  public static void main(String[] args) throws IOException {
        // 准备File对象  找绝对路径
        File file = new File("123.txt");
        // 准备字符输出流
        FileWriter writer = new FileWriter(file);
        // 调用API 写数据
        writer.write(49);
        writer.write("丹丹");
        writer.write(new char[]{'B','C'});
        // 刷新
        writer.flush();
        // 关闭资源
        writer.close();
  }
}

七、综合练习

7.1、文件拷贝操作

private static void copy() throws Exception {
	//1):创建源或者目标对象
	File src = new File("file/copy_before.txt");
	File dest = new File("file/copy_after.txt");
	//2):创建IO流对象
	FileReader in = new FileReader(src);
	FileWriter out = new FileWriter(dest);
	//3):具体的IO操作
	int len = -1;//记录以及读取了多个字符
	char[] buff = new char[1024];//每次可以读取1024个字符
	len = in.read(buff);//先读取一次
	while(len > 0) {
		//边读边写
		out.write(buff, 0, len);
		len = in.read(buff);//再继续读取
	}
	//4):关闭资源(勿忘)
	out.close();
	in.close();
}

7.2、处理异常

private static void copy2() {
	//1):创建源或者目标对象
	File src = new File("file/copy_before.txt");
	File dest = new File("file/copy_after.txt");

	//把需要关闭的资源,声明在try之外
	FileReader in = null;
	FileWriter out = null;
	try {
		//可能出现异常的代码
		//2):创建IO流对象
		in = new FileReader(src);
		out = new FileWriter(dest);
		//3):具体的IO操作
		int len = -1;//记录以及读取了多个字符
		char[] buff = new char[1024];//每次可以读取1024个字符
		len = in.read(buff);//先读取一次
		while (len > 0) {
			out.write(buff, 0, len);
			len = in.read(buff);//再继续读取
		}
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		//4):关闭资源(勿忘)
		try {
			if (out != null) {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (in != null) {
				in.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
           
    //--------------------------------------------
    //ctrl+alt+t
        //Java7的新特性
        //如果资源对象实现了Closeable接口,那么可以不用手动关闭资源了
        //开发者只需要指定需要关闭的资源对象即可
        try (
                //定义需要关闭的资源对象
                FileReader reader = new FileReader("files/before.txt");
                FileWriter writer = new FileWriter("files/after.txt");
                ){
            //从指定文件中读取数据,立即将读到的数据写到指定的文件中
            char[] buffer = new char[1024];
            int len = 0;
            while((len = reader.read(buffer)) != -1){
                //将读到的数据(buffer)写到指定的文件中
                writer.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
}

八、文件流

​ 当程序需要读取文件中的数据或者把数据保存到文件中去,此时就得使用文件流,但是注意只能操作纯文本文件(txt格式),不要使用Word、Excel。文件流比较常用。

8.1、文件字节输出流(FileOutputStream)

private static void test1() throws Exception {
	//1):创建源或者目标对象
	File dest = new File("file/result1.txt");
	//2):创建IO流对象
	FileOutputStream out = new FileOutputStream(dest);
	//3):具体的IO操作
	out.write(65);//输出A
	out.write(66);//输出B
	out.write(67);//输出C
	String str = "to be or not to be";
	out.write(str.getBytes());//输出str字符串中所有内容
	//4):关闭资源(勿忘)
	out.close();
}

8.2、文件字节输入流(FileInputStream)

private static void test2() throws Exception {
	//1):创建源或者目标对象
	File src = new File("file/result1.txt");
	//2):创建IO流对象
	FileInputStream in = new FileInputStream(src);
	//3):具体的IO操作
	System.out.println((char)in.read());//读取A字节
	System.out.println((char)in.read());//读取B字节
	System.out.println((char)in.read());//读取C字节
	byte[] buff = new byte[5];//准备容量为5的缓冲区
	int len = in.read(buff);//读取5个字节数据,并存储到buff数组中
	System.out.println(Arrays.toString(buff));//[116, 111, 32, 98, 101]
	System.out.println(len);//返回读取了几个字节
	//4):关闭资源(勿忘)
	in.close();
}

8.3、文件字符输出流(FileWriter)

private static void test3() throws Exception {
	//1):创建源或者目标对象
	File dest = new File("file/result2.txt");
	//2):创建IO流对象
	FileWriter out = new FileWriter(dest);
	//3):具体的IO操作
	out.write('辛');//输出A
	out.write('弃');//输出B
	out.write('疾');//输出C
	String str = "众里寻他千百度,蓦然回首,那人却在,灯火阑珊处。";
	out.write(str.toCharArray());
	out.write(str);//String的本质就是char[]
	//4):关闭资源(勿忘)
	out.close();
}

8.4、文件字符输入流(FileReader)

private static void test4() throws Exception {
	//1):创建源或者目标对象
	File src = new File("file/result2.txt");
	//2):创建IO流对象
	FileReader in = new FileReader(src);
	//3):具体的IO操作
	System.out.println(in.read());//读取辛字符
	System.out.println(in.read());//读取弃字符
	System.out.println(in.read());//读取疾字符
	char[] buff = new char[5];//准备容量为5的缓冲区
	int len = in.read(buff);//读取5个字符数据,并存储到buff数组中
	System.out.println(Arrays.toString(buff));//[众, 里, 寻, 他, 千]
	System.out.println(len);//返回读取了几个字节
	//4):关闭资源(勿忘)
	in.close();
}

九、缓冲流

​ 节点流的功能都比较单一,性能较低。处理流,也称之为包装流,相对于节点流更高级,这里存在一个设计模式——装饰设计模式。有了包装流之后,我们只关心包装流的操作即可,比如只需要关闭包装流即可,无需在关闭节点流。

​ 四大基流都有自己的包装流

BufferedInputStream / BufferedOutputStream / BufferedReader / BufferedWriter

​ 缓冲流内置了一个默认大小为8192个字节或字符的缓存区,缓冲区的作用用来减少磁盘的IO操作,拿字节缓冲流举例,比如一次性读取8192个字节到内存中,或者存满8192个字节再输出到磁盘中,操作数据量比较大的流,都建议使用上对应的缓存流。

private static void copy3() throws Exception {
	//1):创建源或者目标对象
	File src = new File("file/郭德纲-报菜名.mp3");
	File dest = new File("file/郭德纲-报菜名2.mp3");
	//2):创建IO流对象
	BufferedInputStream bis = 
new BufferedInputStream(new FileInputStream(src), 8192);
	BufferedOutputStream bos = 
new BufferedOutputStream(new FileOutputStream(dest), 8192);
	//3):具体的IO操作
	int len = -1;//记录以及读取了多个字符
	byte[] buff = new byte[1024];//每次可以读取1024个字符
	len = bis.read(buff);//先读取一次
	while (len > 0) {
		//边读边写
		bos.write(buff, 0, len);
		len = bis.read(buff);//再继续读取
	}
	//4):关闭资源(勿忘)
	bos.close();
	bis.close();
}

十、字节流和字符流如何选用

​ 使用记事本打开某个文件,如果可以看到内容的就是文本文件,否则可以暂时认为是二进制格式的。

​ 一般的,操作二进制文件(图片、音频、视频等)必须使用字节流操作文本文件使用字符流,尤其是操作带有中文的文件,使用字符流不容易导致乱码,因为使用字节流可能出现读取半个汉字的尴尬(汉字由两个或三个字节组成)。当然,如果不清楚属于哪一类型文件,都可以使用字节流。

十一、对象序列化

​ 序列化是指把Java堆内存中的对象数据,通过某种方式把对象数据存储到磁盘文件中或者传递给给网络上传输。序列化在分布式系统在应用非常广泛。

​ 相反的也有反序列化:把磁盘文件中的对象的数据或者把网络节点上的对象数据恢复成Java对象的过程。

​ 如果想做序列化的类必须实现序列化接口java.io.Serializable(这是标志接口,里面并没有抽象方法),如果字段使用transient 修饰则不会被序列化。