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-10-27 19:01:51 发布