IO stream basics-writing and writing of character streams

IO style

IO: Data writing and writing, (data persistence) adopts different streams for different characteristics of operating data.

Use of the File class

An object of the File class represents a file or a file directory.

//path 可以是绝对路径也可以是相对路径
File file= new File(path);

Regarding the relative path, the relative path of different IDEs is different.
The relative paths of Eclipse and IDEA are different.

  • IDEA's relative path:
    If you use the unit test method in Junit for testing, the relative path is under Module;
    if you use main() to test, the relative path is in the current project.

  • Path separator
    In different OS, the separator is different.

  • Common methods of FIle classRed is a common method

IO flow principle and flow classification

IO is a very used technology for processing data transfer between devices. Such as reading and writing files, network communication, etc.

The operation of data is carried out in a stream.

  • File to program: input

  • Program to file: output

  • Network to memory: input

  • Memory to network: output

  • Memory to database: output

The programmer's position is the program, that is, the memory. Only when the position is correct can the correct class be selected.

Classification of flow:

According to different data units

  • Byte stream 8bit byte
  • Character stream 16bit char

Character streams are more suitable for processing text. The byte stream is suitable for processing pictures, videos, audios, etc.

According to different roles:

  • Node stream (act directly on the file class)
  • Processing flow (acting on the basis of existing flow)

Divide according to the flow direction

  • Input stream
  • Output stream

Abstract class of stream

Remember these four, you can distinguish all flows
The blue ones need to be focused on

  • Character reading
//1.实例化File类对象,指明要操作的文件
File file = new File("hello.txt");

//2.提供具体的流
FileReader fr = new FileReader(file);
//3.数据的读入
//read();返回读入的一个字符,如果达到文件末尾就返回-1
//无参数的read()方法,每次读入一个字符。
int data = fr.read();//将字符转化为int值
while(data != -1){
    
    
	System.out.print((char)data);
	data = fr.read();
}
//流的关闭
fr.close();

In order to ensure that the resources of the stream can be closed, try catch finally is used for exception handling, and the stream is closed in finally.

Summary: For data reading operations, it is necessary to ensure that the file being read must exist, otherwise FileNotFoundException will be reported.

Upgrade the operation of read(), use the overload method of read

File file = new File("hello.txt");
//1.File类的实例化

//2. XXX流的实例化
FileReader fr = new FileReader(file);
//3. 读入的操作
//每次读入的字符的个数。
char[] cbuf = new char[5];
int len;
while((len = fr.read(cbuf)) != -1){
    
    
	//错误写法:
	//for(int i = 0; i<cbuf.length;i++){
    
    
	//	System.out.print(cbuf[i]);
	//}
	//正确写法
	for(int i = 0;i<len;i++){
    
    
		System.out.print(cbuf[i]);
	}
	//另一种写法
	String str = new String(cbuf,0,len);
	System.out.print(str);
}
//4. 流的关闭

Write data from the memory to a file on the hard disk

//1.实例化File类对象,指明要写出到的文件
File file = new File("hello.txt");

//2.提供fileWriter对象,用于文件的写出
FileWriter fw = new FileWriter(file);
//3.写出操作
fw.witer("i have a dream");
//4.流资源的关闭
fw.close();

Summary: If the file does not exist, it will be created automatically during the input process.
If it exists: If the constructor used by the stream is FileWriter(file,false); it will not be overwritten, if FileWriter(file) is used, the original file will be overwritten.

Read in first and write out

@Test
    public void  method() throws IOException {
    
    

        //指明读入文件的位置
        File fileRead = new File("hello.txt");
        //指明写出文件的位置
        File fileWriter = new File("hello1.txt");
        //创建输入流和输出流对象
        FileReader fr = new FileReader(fileRead);
        FileWriter fw = new FileWriter(fileWriter);

        //创建字符数组
        int len;
        char[] chars = new char[5];
        while ((len = fr.read(chars))!= -1){
    
    
            //每次写入len个长度的字符
            fw.write(chars,0,len);
        }
        //关闭流
        fr.close();
        fw.close();
    }

In practical applications, try catch finally is used to ensure that the stream is closed.

Guess you like

Origin blog.csdn.net/weixin_43941676/article/details/108329943