public static void main(String[] args) throws IOException { InputStream is = new FileInputStream("Day09\\src\\denn"); byte[] bytes = new byte[3]; int len; while (( len = is.read() ) != -1) { System.out.println(new String(bytes, 0, len)); } is.close(); }
时间: 2024-04-27 10:22:36 浏览: 65
这段代码是一个 Java 程序,它打开了一个名为 "denn" 的文件,从该文件中读取每个字节,并将其转换为字符串输出到控制台。具体来说,它使用 InputStream 和 FileInputStream 类来读取文件内容,每次读取 3 个字节,然后使用 String 类将这些字节转换为字符串,并使用 System.out.println() 方法将其输出到控制台。最后,它关闭了输入流。
相关问题
InputStream.read
(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();
}
}
}
阅读全文