Java系列教程:文件IO操作

1.什么是文件IO操作呢?
文件的IO操作将磁盘文件中的数据读取到内存中或者将内存中的数据写回磁盘文件的操作.
对于FileInputStream,主要是将磁盘文件中的数据读入到用户态JVM堆内存的过程,具体会先从磁盘文件拷贝到内核态的内存(RAM)中,然后再将内核态内存中的数据拷贝到用户态的内存空间,对于Java主要是指拷贝到JVM堆中的过程.
对于FileOutputStream,主要是指将内核态内存(JVM堆)中的数据写回磁盘文件的过程,具体会先将用户态内存的数据拷贝回内核态的内存(RAM),然后再写回磁盘文件的过程.
Java系列教程:文件IO操作_第1张图片
2.常用IO流以及具体使用
从操作数据是属于字节还是字符可以分为字符流和字节流两大类,字节流的父类主要是指继承自InputStream和OutputStream的所有子类.而字符流主要指继承自Reader和Writer的所有子类.

字符流和字节流的区别
字符流: 操作的基本单元为Unicode码元,使用缓冲区,
字节流: 操作的基本单位为字节,默认不使用缓存区。
Java系列教程:文件IO操作_第2张图片Java系列教程:文件IO操作_第3张图片

字节流常用方法

方法 描述
read() 从InputStream流读取下一个字节的数据
read(byte b[]) 从InputStream中读取0到byte数组长度的数据并保存到byte数组中,最多读取byte数组长度数据,如果数据不够byte数组长度,则读取实际数据大小
read(byte b[], int off, int len) 读取0到len个数据并保存到byte数组中,其中第一个数据将保存到byte[off]位置,具体读取多少数据以实际数据多少为主,最多读取len数据
close() 关闭与文件或其他IO设备的流

字符流常用方法

方法 描述
read() 读取单个字符数据
read(java.nio.CharBuffer target) 读取数据到CharBuffer
read(char cbuf[]) 读取数据到字符数组中
read(char cbuf[], int off, int len) 读取数据到字符数组中,同时从off开始存储,最多存储len个字符
close() 关闭与字符流

常用流介绍

  1. 文件流相关
    FileInputStream:主要从文件中读取数据
    FileOutputStream:主要将内存中的数据写回文件
    从文件读取数据
public class FileDemo {
    public static void main(String[] args) throws Exception{
       InputStream inputStream = new FileInputStream("a.txt");
       byte[] arr = new byte[1024];
       while(true){
           int len = inputStream.read(arr);
           if(len == -1){
               break;
           }
           System.out.println(new String(arr, "utf-8"));
       }
    }
}

将数据写回文件

public class FileDemo {
    public static void main(String[] args) throws Exception{
        OutputStream outputStream = new FileOutputStream("a.txt");

        String writeToFileStr = "Hi, I am doing the test for writing data to the file";
        outputStream.write(writeToFileStr.getBytes("utf-8"));

        outputStream.close();
    }
}

使用字节流操作文件
使用BufferedReader读取数据

public class FileDemo {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("a.txt")));
        String line = br.readLine();
        while(line != null){
            line = br.readLine();
            System.out.println(line);
        }
    }

使用BufferedWriter数据

public class FileDemo {
    public static void main(String[] args) throws Exception{
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("a.txt")));
        bufferedWriter.write("hello, I am doing the test");
        bufferedWriter.write("\n");
        bufferedWriter.write("The second line");
        bufferedWriter.flush();
        bufferedWriter.close();
    }
}

  1. BufferedReader 跟 BufferedWriter
    这两个类会使用一个内部缓冲数组一次性对指定缓存大小的数据进行缓存,以后每次操作数据时优先从缓冲区读取或写入缓存区,直到缓冲区数据不够时,重新与原始流进行交互,从文件读取数据或将数据写入文件
    BufferedReader:数据读取过程
    如下图所示,当我们读取数据的时候我们会优先读取缓冲数组大小的数据到内存中,然后read()方法调用时直接从缓冲数组获取数据,当缓冲数组大小数据还不够时,则重新将文件的数据再次一次性读取缓冲数组大小的数据到缓冲数组,这样能提高读取数据
    Java系列教程:文件IO操作_第4张图片
    BufferedWriter: 数据写入过程
    当我们进行输入写入时,由于BufferedWriter内部有一个缓冲数组,所以当数组没写满之前,所有的数组都是写入到缓冲数组中,只有当数组满了或者不够写的时候就会将数组中的数据写回到内存当中,这也就是为什么我们写完数据之后需要手动的调用flush()函数确保数据写回io设备如文件中。
    Java系列教程:文件IO操作_第5张图片

  2. InputStreamReader和OutputStreamWriter
    InputStreamReader: 是InputStream和Reader之间的桥梁,用于将字节流转换成字符流进行操作.他将读取字节流数据,同时根据Charset(字符编码)进行解码并将字节流转换成字符流
    OutputStreamWriter: 是OutputStream和Writer之间的桥梁,用于将字符流转换成字节流进行操作。具体会先将字符流根据指定的Charset(字符编码)进行编码并将字符流转换成字节流.

你可能感兴趣的:(JAVA,java)