package com.java;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IOTest {
/**
* 字节流操作控制台输入输出
*/
public static void main(String[] args) {
// 输入字节流
InputStream in = System.in;
// 输出字节流
OutputStream out = System.out;
try {
// 单个字符的循环输入输出
oneWordIO(in, out);
// 一行一行循环输入输出
oneLineIO(in, out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 一行一行循环输入输出
*/
private static void oneLineIO(InputStream in, OutputStream out)
throws IOException {
byte[] buf = new byte[1024];
int len = -1;
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
out.flush();
}
}
/**
* 单个字符的输入输出
*/
private static void oneWordIO(InputStream in, OutputStream out)
throws IOException {
int ch = -1;
while ((ch = in.read()) != -1) {
out.write(ch);
out.flush();
}
}
}
java IO总结之字节流操作控制台输入输出
最新推荐文章于 2022-04-19 10:58:29 发布
本文介绍了一个Java程序示例,演示了如何使用Java的输入输出流进行控制台的数据读写操作。通过两个方法分别实现了单个字符的循环输入输出及按行循环输入输出的功能。
4167

被折叠的 条评论
为什么被折叠?



