InputStream.read
时间: 2023-09-17 16:09:36 浏览: 90
InputStream
4星 · 用户满意度95%
(InputStream.java)
The read() method of the InputStream class reads a single byte of data from the input stream and returns it as an integer value. If the end of the stream is reached, the method returns -1.
Syntax:
public abstract int read() throws IOException
Parameters:
This method does not take any parameters.
Return Value:
Returns an integer value that represents the byte read from the input stream. If the end of the stream is reached, the method returns -1.
Exceptions:
This method throws an IOException if an I/O error occurs while reading from the input stream.
Example:
The following example reads a file using a FileInputStream object and prints the contents of the file to the console.
import java.io.*;
public class ReadFileExample {
public static void main(String[] args) {
try {
FileInputStream inputStream = new FileInputStream("file.txt");
int data;
while ((data = inputStream.read()) != -1) {
System.out.print((char) data);
}
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
阅读全文