JAVA review of I/0 (input/output)

Insert picture description here

1. What is flow?

Insert picture description here

2. What is an input stream?

There are two input stream abstract classes, InputStream byte input stream and Reader character input stream
Insert picture description here

(1) InputStream class

The InputStream class is an abstract class of byte input streams, and is the parent class of all byte input streams;
the specific structure level of the
Insert picture description here
class is as follows: All methods in the InputStream class will raise IOException when encountering an error. Common methods and descriptions of this class As shown in the table:
Insert picture description here
Note: Not all subclasses of the inputStream class support the methods defined in inputStream. The skip(), mark(), reset() and other methods are only useful for some subclasses.

(Two) Reader class

The characters in java are Unicode encoding, double bytes, and the InputStream class is used to output single bytes, which is not suitable for processing characters. For this reason, java provides a Reader class specifically used to process characters. The Reader class is a character The abstract class of the input stream is also the parent class of all character input streams. The specific hierarchical structure of the Reader class is as follows:
Insert picture description here
Note:
The parameter of the read() method of the Reader class is an array of char type. In addition, the Reader class also provides a ready() method, which is used to determine whether to read the stream, and its return value is of type boolean.

3. What is an output stream?

There are also two types of output stream abstract classes, namely OutputStream byte output stream and Writer character output stream
Insert picture description here

(1) OutputStream class

The OutputStream class is an abstract class of byte output streams and the parent class of all byte output streams. The specific levels of the
Insert picture description here
OutputStream class are as follows: All methods in the OutputStream class have no return value, and an IOException will be raised when an error is encountered. Common methods and descriptions of the class:
Insert picture description here

(Two) Writer class

The writer class is an abstract class of character output streams and the parent class of all character output streams. The hierarchy of the Writer class is as follows:
Insert picture description here
The common methods and descriptions of the Writer class are as follows:
Insert picture description here

Fourth, the File class

(1) Create a file object

Insert picture description here

(Two) the use of File class

Insert picture description here

(3) File operation

Insert picture description here

(3) Folder operation

Insert picture description here

Five, file input/output stream

During the running of the program, most of the data is stored in the memory. When the program ends or is closed, the data stored in the memory will disappear. If you need to save the data permanently, the best way is to save the data to a disk file Provide the following classes for this

(1) FileInputStream class and FileOutputStream class

FileInputStream class: used to read the content of the file;
FileOutputStream class: used to write the content to the file

Case analysis:

public class FileStreamTest {
    
    
	public static void main(String[] args) {
    
    
		File file = new File("word.txt"); // 创建文件对象
		try {
    
     // 捕捉异常
			// 创建FileOutputStream对象,用来向文件中写入数据
			FileOutputStream out = new FileOutputStream(file);
			// 定义字符串,用来存储要写入文件的内容
			String content = "你见过洛杉矶凌晨4点的样子吗?";
			// 创建byte型数组,将要写入文件的内容转换为字节数组
			byte buy[] = content.getBytes();
			out.write(buy); // 将数组中的信息写入到文件中
			out.close(); // 将流关闭
		} catch (IOException e) {
    
     // catch语句处理异常信息
			e.printStackTrace(); // 输出异常信息
		}
		try {
    
    
			// 创建FileInputStream对象,用来读取文件内容
			FileInputStream in = new FileInputStream(file);
			byte byt[] = new byte[1024]; // 创建byte数组,用来存储读取到的内容
			int len = in.read(byt); // 从文件中读取信息,并存入字节数组中
			// 将文件中的信息输出
			System.out.println("文件中的信息是:" + new String(byt, 0, len));
			in.close(); // 关闭流
		} catch (Exception e) {
    
    
			e.printStackTrace();
		}
	}
}

(2) File character stream: FileReader class and FileWriter class

Use character stream to avoid garbled characters when writing files.
Insert picture description here
Case analysis:

public class Demo1 {
    
    
    public static void main(String[] args) {
    
    

        File f = new File("E:\\untitled\\src\\cn\\tx\\Exceriser200\\text005\\word.txt");

        /*
        FileWriter fw = null;

        String st = "天行健,自强不息;地势坤,厚德载物";
        try {
            //加true  表示在源文件后追加新内容
            fw = new FileWriter(f,true);
            fw.write(st);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fw != null){
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }*/

        FileReader fr = null;

        try {
    
    
            fr = new FileReader(f);
            //创建缓冲区  读取数据
            char ch[] = new char[1024];
            //已经读出的字符数
            int count;
            //循环读取文件中的数据 直到所有字符都读完
            while((count = fr.read(ch))!=-1){
    
    
                System.out.println("文件中的内容为:"+new String(ch,0,count));
            }

        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if(fr != null){
    
    
                try {
    
    
                    fr.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

Five, buffered input/output stream

(一)BufferedInputStream与BufferedOutputStream

Insert picture description here
Case analysis:

public class BufferedStreamTest {
    
    
    public static void main(String args[]) {
    
    
        // 定义字符串数组
        String content[] = {
    
     "你不喜欢我,", "我一点都不介意。", "因为我活下来,", "不是为了取悦你!" };
        File file = new File("word.txt"); // 创建文件对象
        FileOutputStream fos = null; // 创建FileOutputStream对象
        BufferedOutputStream bos = null; // 创建BufferedOutputStream对象
        FileInputStream fis = null; // 创建FileInputStream对象
        BufferedInputStream bis = null; // 创建BufferedInputStream对象
        try {
    
    
            fos = new FileOutputStream(file); // 实例化FileOutputStream对象
            bos = new BufferedOutputStream(fos); // 实例化BufferedOutputStream对象
            byte[] bContent = new byte[1024]; // 创建可以容纳1024个字节数的缓冲区
            for (int k = 0; k < content.length; k++) {
    
     // 循环遍历数组
                bContent = content[k].getBytes(); // 将遍历到的数组内容转换为字节数组
                bos.write(bContent); // 将字节数组内容写入文件
            }
            System.out.println("写入成功!\n");
        } catch (IOException e) {
    
     // 处理异常
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                bos.close(); // 将BufferedOutputStream流关闭
                fos.close(); // 将FileOutputStream流关闭
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
        try {
    
    
            fis = new FileInputStream(file); // 实例化FileInputStream对象
            bis = new BufferedInputStream(fis); // 实例化BufferedInputStream对象
            byte[] bContent = new byte[1024]; // 创建byte数组,用来存储读取到的内容
            int len = bis.read(bContent); // 从文件中读取信息,并存入字节数组中
            // 输出文件数据
            System.out.println("文件中的信息是:" + new String(bContent, 0, len));
        } catch (IOException e) {
    
     // 处理异常
            e.printStackTrace();
        } finally {
    
    
            try {
    
    
                bis.close(); // 将BufferedInputStream流关闭
                fis.close(); // 将FileInputStream流关闭
            } catch (IOException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

Insert picture description here

(二)BufferedReader与BufferedWriter

Insert picture description here

1. Common methods
Insert picture description here
of BufferedReader class 2. Common methods of BufferedWriter class
Insert picture description here
3. Case analysis

public class Demo4 {
    
    
    public static void main(String[] args) {
    
    
        File file = new File("word.txt");
        /*   写入内容到文件里   */
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
    
    
            fw = new FileWriter(file);
            //将文本字符输出流包装成缓冲字符流
            bw = new BufferedWriter(fw);

            String str1 = "世界这么大";
            String str2 = "我想去看看";
            //第一行的数据
            bw.write(str1);
            //创建新行
            bw.newLine();
            //第二行的数据
            bw.write(str2);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
            //要注意流的关闭顺序,先创建后关闭
            if(bw != null){
    
    
                try {
    
    
                    bw.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
            if(fw != null){
    
    
                try {
    
    
                    fw.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
        /*   读取文件里内容   */
        FileReader fr = null;
        BufferedReader br = null;
        try {
    
    
            fr = new FileReader(file);
            //将文本字符输入流包装成缓冲字符输入流
            br = new BufferedReader(fr);
            String tem = null;
            int i = 1;
            while ((tem = br.readLine()) != null){
    
    
                System.out.println("第"+i+"行:"+tem);
                i++;
            }
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if(br !=null){
    
    
                try {
    
    
                    br.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
            if(fr != null){
    
    
                try {
    
    
                    fr.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}

6. Difficulties

(1) The difference between byte stream and character stream

The byte stream ends with Stream, and the character stream ends with Reader or Writer. The character stream is read and written according to characters, and all are good at processing text information. The byte stream is read and written according to the smallest byte, so the byte stream can handle all data streams, but the function is not as much as the character stream.

(2) Use of input/output streams

Taking the memory as the reference object, when data flows from the file to the memory, the input stream is required; when the data flows from the memory to the file, the output stream is required (printing the data on the display is one kind)

Guess you like

Origin blog.csdn.net/javaScript1997/article/details/108907468