JAVA 数据流 编写程序,使用Java标准数据流,将键盘上输入的字符在屏幕上显示出来。
时间: 2024-03-02 07:51:51 浏览: 114
可以使用Java标准数据流中的 `System.in` 和 `System.out` 实现这个功能。以下是示例代码:
```java
import java.io.*;
public class DataStreamExample {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String input;
while ((input = reader.readLine()) != null) {
writer.write(input);
writer.newLine();
writer.flush();
}
}
}
```
这个程序创建了一个 `BufferedReader` 和一个 `BufferedWriter`,分别用于从标准输入读取数据和向标准输出写入数据。在 `while` 循环中,程序不断从输入流中读取一行数据,并将其写入输出流中。注意在写入数据时要使用 `newLine()` 方法来换行,并调用 `flush()` 方法确保数据被立即写入输出流中。
阅读全文
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)