javase personal trash review notes 10 Java Stream, File and IO

Reading console input
The following is the basic syntax for creating a BufferedReader:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Read multi-character input from the console

int read( ) throws IOException

Each time the read() method is called, it reads a character from the input stream and returns the character as an integer value. When the stream ends, -1 is returned. This method throws IOException.
The following program demonstrates using the read() method to continuously read characters from the console until the user enters "q".

//使用 BufferedReader 在控制台读取字符
 
import java.io.*;
 
public class BRRead {
    
    
    public static void main(String args[]) throws IOException {
    
    
        char c;
        // 使用 System.in 创建 BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("输入字符, 按下 'q' 键退出。");
        // 读取字符
        do {
    
    
            c = (char) br.read();
            System.out.println(c);
        } while (c != 'q');
    }
}
/*以上实例编译运行结果如下:

输入字符, 按下 'q' 键退出。
runoob
r
u
n
o
o
b


q
q


Reading a string from the console Reading a string from standard input requires the readLine() method of BufferedReader.

Its general format is:
String readLine() throws IOException
The following program reads and displays character lines until you enter the word "end".

//使用 BufferedReader 在控制台读取字符
import java.io.*;
 
public class BRReadLines {
    
    
    public static void main(String args[]) throws IOException {
    
    
        // 使用 System.in 创建 BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        System.out.println("Enter lines of text.");
        System.out.println("Enter 'end' to quit.");
        do {
    
    
            str = br.readLine();
            System.out.println(str);
        } while (!str.equals("end"));
    }
}
/*以上实例编译运行结果如下:

Enter lines of text.
Enter 'end' to quit.
This is line one
This is line one
This is line two
This is line two
end
end

Console output
The following example uses write() to output the character "A" and the following newline to the screen:

import java.io.*;
 
//演示 System.out.write().
public class WriteDemo {
    
    
    public static void main(String args[]) {
    
    
        int b;
        b = 'A';
        System.out.write(b);
        System.out.write('\n');
    }
}
/*运行以上实例在输出窗口输出 "A" 字符

A

The write() method is not often used because the print() and println() methods are more convenient to use.

Read and write files
Insert picture description here
FileInputStream

You can use string type file names to create an input stream object to read files:

InputStream f = new FileInputStream("C:/java/hello");
You can also use a file object to create an input stream object to read files. We first have to use the File() method to create a file object:

File f = new File(“C:/java/hello”);
InputStream out = new FileInputStream(f);

After creating the InputStream object, you can use the following methods to read the stream or perform other stream operations.

Serial number method and description
1 public void close() throws IOException{}
Close this file input stream and release all system resources related to this stream. Throw IOException.
2 protected void finalize()throws IOException {}
This method clears the connection with the file. Make sure to call its close method when the file input stream is no longer referenced. Throw IOException.
3 public int read(int r)throws IOException{}
This method reads the specified byte of data from the InputStream object. The return is an integer value. Return the next byte of data, or -1 if it has reached the end.
4 public int read(byte[] r) throws IOException{}
This method reads r.length bytes from the input stream. Returns the number of bytes read. If it is the end of the file, -1 is returned.
5 public int available() throws IOException{}
Returns the number of bytes that can be read from this input stream without blocking the next method called on this input stream. Returns an integer value.

//It’s too difficult, I will make up later.

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108553007