JAVA_IO stream_byte stream character stream notes book

1 byte output stream OutputStream

Member method

1.  java.io.OutputStream:字节输出流
        此抽象类是表示输出字节流的所有类的超类。

2.  定义了一些子类共性的成员方法:
    `public void close()`:关闭此输出流并释放与此流相关联的任何系统资源。
    ` public void flush() `:刷新此输出流并强制任何缓冲的输出字节被写出。
    `public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
    ` public void write(byte[] b, int off, int len) `:从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
    ` public abstract void write(int b) `:将指定的字节输出流。

3.  java.io.FileOutputStream extends OutputStream
    FileOutputStream:文件字节输出流
    作用:把内存中的数据写入到硬盘的文件中

    构造方法:
        FileOutputStream(String name)创建一个向具有指定名称的文件中写入数据的输出文件流。
        FileOutputStream(File file) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
        参数:写入数据的目的
            String name:目的地是一个文件的路径
            File file:目的地是一个文件
        构造方法的作用:
            1.创建一个FileOutputStream对象
            2.会根据构造方法中传递的文件/文件路径,创建一个空的文件
            3.会把FileOutputStream对象指向创建好的文件

3. 写入数据的原理(内存-->硬盘)
        java程序-->JVM(java虚拟机)-->OS(操作系统)-->OS调用写数据的方法-->把数据写入到文件中
4. 字节输出流的使用步骤(重点):
        1.创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
        2.调用FileOutputStream对象中的方法write,把数据写入到文件中
        3.释放资源(流使用会占用一定的内存,使用完毕要把内存清空,提供程序的效率)
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo01OutputStream {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileOutputStream fos = new FileOutputStream("09_IOAndProperties\\a.txt");//第一步
        fos.write(97);//第二步,系统中写入字母a
        fos.close();//第三步
    }
}

How to write multiple bytes at once

public void write(byte[] b): Write b.length bytes from the specified byte array to this output stream.
public void write(byte[] b, int off, int len): Write len bytes from the specified byte array, and output to this output stream from offset off.
              If the first byte written is a positive number (0-127), then the ASCII table will be queried when displayed.
              If the first byte written is a negative number, then the first byte will be the same as the second byte. Bytes form a Chinese display, query the system default code table (GBK)

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class Demo02OutputStream {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileOutputStream fos = new FileOutputStream(new File("09_IOAndProperties\\b.txt"));
        //在文件中显示100,写个字节
        fos.write(49);
        fos.write(48);
        fos.write(48);


        byte[] bytes = {
    
    65,66,67,68,69};//ABCDE
        //byte[] bytes = {-65,-66,-67,68,69};//烤紻E
        fos.write(bytes);
        fos.write(bytes,1,2);//BC

        /*
            写入字符的方法:可以使用String类中的方法把字符串,转换为字节数组
                byte[] getBytes()  把字符串转换为字节数组
         */
        byte[] bytes2 = "你好".getBytes();
        System.out.println(Arrays.toString(bytes2));//[-28, -67, -96, -27, -91, -67]
        fos.write(bytes2);

        //释放资源
        fos.close();
    }
}

Construction method using two parameters

import java.io.FileOutputStream;
import java.io.IOException;

/*
    追加写/续写:使用两个参数的构造方法
        FileOutputStream(String name, boolean append)创建一个向具有指定 name 的文件中写入数据的输出文件流。
        FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
        参数:
           String name,File file:写入数据的目的地
           boolean append:追加写开关
            true:创建对象不会覆盖源文件,继续在文件的末尾追加写数据
            false:创建一个新文件,覆盖源文件
    写换行:写换行符号
        windows:\r\n
        linux:/n
        mac:/r
 */
public class Demo03OutputStream {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileOutputStream fos = new FileOutputStream("09_IOAndProperties\\c.txt",true);
        for (int i = 1; i <=10 ; i++) {
    
    
            fos.write("你好".getBytes());
            fos.write("\r\n".getBytes());
        }

        fos.close();
    }
}

2 byte input stream InputStream

Member method

 1. java.io.InputStream:字节输入流
    此抽象类是表示字节输入流的所有类的超类。

 2. 定义了所有子类共性的方法:
         `int read()`从输入流中读取数据的下一个字节。
         `int read(byte[] b) `从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
        ` void close()` 关闭此输入流并释放与该流关联的所有系统资源。

 3. java.io.FileInputStream extends InputStream
    FileInputStream:文件字节输入流
    作用:把硬盘文件中的数据,读取到内存中使用

 4. 构造方法:
        FileInputStream(String name)
        FileInputStream(File file)
        参数:读取文件的数据源
            String name:文件的路径
            File file:文件
        构造方法的作用:
            1.会创建一个FileInputStream对象
            2.会把FileInputStream对象指定构造方法中要读取的文件

  5. 读取数据的原理(硬盘-->内存)
        java程序-->JVM-->OS-->OS读取数据的方法-->读取文件

  6. 字节输入流的使用步骤(重点):
        1.创建FileInputStream对象,构造方法中绑定要读取的数据源
        2.使用FileInputStream对象中的方法read,读取文件
        3.释放资源

Read one byte at a time

import java.io.FileInputStream;
import java.io.IOException;
public class Demo01InputStream {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileInputStream fis = new FileInputStream("09_IOAndProperties\\c.txt");
        /*
            发现以上读取文件是一个重复的过程,所以可以使用循环优化
            不知道文件中有多少字节,使用while循环
            while循环结束条件,读取到-1的时候结束

            布尔表达式(len = fis.read())!=-1
                1.fis.read():读取一个字节
                2.len = fis.read():把读取到的字节赋值给变量len
                3.(len = fis.read())!=-1:判断变量len是否不等于-1
				
			如果不赋值给len
			while(fis.read()!=-1){
				System.out.print(fis.read());
        }
			则不对
         */
        int len = 0; //记录读取到的字节
        while((len = fis.read())!=-1){
    
    
            System.out.print(len);//abc
        }

        //3.释放资源
        fis.close();
    }
}

Read multiple bytes at once

 1. 字节输入流一次读取多个字节的方法:
        int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
    明确两件事情:
        1.方法的参数byte[]的作用?
            起到缓冲作用,存储每次读取到的多个字节
            数组的长度一把定义为1024(1kb)或者1024的整数倍
        2.方法的返回值int是什么?
            每次读取的有效字节个数

 2. String类的构造方法
        String(byte[] bytes) :把字节数组转换为字符串
        String(byte[] bytes, int offset, int length) 把字节数组的一部分转换为字符串 offset:数组的开始索引 length:转换的字节个数
import java.io.FileInputStream;
import java.io.IOException;
public class Demo02InputStream {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileInputStream fis = new FileInputStream("09_IOAndProperties\\b.txt");
        /*
            发现以上读取时一个重复的过程,可以使用循环优化
            不知道文件中有多少字节,所以使用while循环
            while循环结束的条件,读取到-1结束
         */
        byte[] bytes = new byte[1024];//存储读取到的多个字节
        int len = 0; //记录每次读取的有效字节个数
        while((len = fis.read(bytes))!=-1){
    
    
            //String(byte[] bytes, int offset, int length) 把字节数组的一部分转换为字符串 offset:数组的开始索引 length:转换的字节个数
            System.out.println(new String(bytes,0,len));
        }
        //释放资源
        fis.close();
    }
}

byte array storage

Storage rules: It is an overwriting behavior. When the length of the array is not enough, if there are bytes that can be stored, it will be overwritten; if there is no byte, the original will remain unchanged

public class demo02 {
    
    
    public static void main(String[] args) throws IOException {
    
    
        FileInputStream fis = new FileInputStream("09_IOAndProperties\\src\\com\\itheima\\demo02\\InputStream\\zyx.txt");
        byte[] bytes = new byte[3];
        int i1 = fis.read(bytes);
        System.out.println(i1);//3
        System.out.println(Arrays.toString(bytes));//[122, 121, 120]
        System.out.println(new String(bytes));//zyx

        int i2 = fis.read(bytes);
        System.out.println(i2);
        System.out.println(new String(bytes));
        fis.close();
    }}
//结果
[122, 121, 120]
zyx
-1
zyx

Guess you like

Origin blog.csdn.net/TOPic666/article/details/107953858