用FileInputStream和FileOutputStream,用键盘输入信息到“data.txt”文件中,再显示到屏幕上
时间: 2024-05-04 22:22:35 浏览: 62
以下是Java代码实现:
```java
import java.io.*;
public class FileInputOutputExample {
public static void main(String[] args) throws IOException {
// 从键盘读取输入
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入要保存到文件中的内容:");
String input = reader.readLine();
// 将输入写入文件
FileOutputStream fos = new FileOutputStream("data.txt");
fos.write(input.getBytes());
fos.close();
// 从文件中读取内容并输出到屏幕上
FileInputStream fis = new FileInputStream("data.txt");
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
System.out.println("文件中的内容是:" + new String(bytes));
fis.close();
}
}
```
运行程序后,会提示用户输入要保存到文件中的内容。用户输入完成后,程序将输入内容写入名为“data.txt”的文件中,并从该文件中读取内容并输出到屏幕上。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)