当前位置:网站首页>Basic use of BufferedReader and bufferedwriter

Basic use of BufferedReader and bufferedwriter

2022-06-11 06:01:00 Not bald

Bufferedreader And Bufferedwriter Basic use of

import java.io.*;

public class IOTest {
    

    public static void main(String[] args) {
    
        // Efficient buffered input stream 
        BufferedReader reader = null;
        try {
    
            // What's coming in is reader Subclasses of 
            reader = new BufferedReader(new FileReader("test.txt"));
            String line;
            // If there is another line, just keep reading readLine() Used to read a line 
            while ((line = reader.readLine()) != null) {
    
                System.out.println(line);
            }
        } catch (Exception e) {
    
            e.printStackTrace();
        } finally {
    
            if (reader != null) {
    
                try {
    
                    reader.close();
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
            }
        }
        // Efficient buffered output stream 
        BufferedWriter writer = null;
        BufferedReader reader1;
        try {
    
            // Create an efficient buffered output stream 
            writer = new BufferedWriter(new FileWriter("test1.txt"));
            // Create an efficient buffered input stream 
            reader1 = new BufferedReader(new FileReader("test.txt"));
            String line;
            while ((line = reader1.readLine()) != null) {
    
                writer.write(line);
                writer.newLine();
                writer.flush();

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

BufferedReader It's a buffered character input stream . It is inherited from Reader.BufferReader Will be the Reader The data in is read in batches , Read a portion at a time into the buffer ; After this part of the buffer has been manipulated , Again from Reader To read the data for the next section .
BufferedWriter It's a buffered character output stream . It is inherited from Writer.BufferedWriter To add some buffering to other character output streams .BufferedWriter Buffer data through character arrays , When the buffer is full or the user calls flush() Function time , It writes the buffer data to the output stream .

Bufferedreader

Usually use while((***.readLine)!= null){} To read as characters . as follows :

while ((result = br.readLine()) != null) {
    
				System.out.println(" Read file =" + result);
				bw.write(result);
			}

At the same time, it also has the function of comparing the contents in the file with the obtained contents
for example
if (byt.compareTo(name+" "+password) == 0)

Bufferedwriter

 BufferedWriter bw = new BufferedWriter(newFileWriter("student.txt"));
        bw.write("this is my student.txt");
        bw.newLine();
        bw.write("xixi");
        bw.close();

Bufferedwriter The common methods of are the above methods ,write write in ,newLine Line break .

原网站

版权声明
本文为[Not bald]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020530437512.html