1、(FileInputStream类的应用)编写一个Java程序,在主方法main()中创建FileInputStream类的实例,使它能够打开文件myfile.txt,并能够把文件的内容显示在屏幕上;
时间: 2024-03-01 11:54:21 浏览: 28
好的,根据您的要求,我来给您展示一个实现读取文件并显示在屏幕上的Java程序:
```java
import java.io.*;
public class FileReadDemo {
public static void main(String[] args) {
try {
// 创建文件输入流
FileInputStream fis = new FileInputStream("myfile.txt");
// 创建缓冲区
byte[] buffer = new byte[1024];
int len;
// 读取文件输入流中的数据,并输出到屏幕上
while ((len = fis.read(buffer)) != -1) {
String str = new String(buffer, 0, len);
System.out.print(str);
}
// 关闭文件输入流
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
在这个程序中,我们首先创建了一个`FileInputStream`对象,用来打开文件`myfile.txt`。然后,我们使用一个缓冲区来读取文件输入流中的数据,并将其输出到屏幕上。最后,我们关闭文件输入流,完成文件读取操作。