功能:读取控制台输入
缺陷:控制台输入中文有点异常;未使用DataInputStream专属接口,如readInt
package com.cool.io;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamTest {
public static void main(String[] args) {
InputStream in = null;
DataInputStream din = null;
try {
in = System.in;
din = new DataInputStream(in);
byte[] b = new byte[1024];
int len;
while ((len = din.read(b)) > 0) {
System.out.println("len: " + len);
System.out.println("content: " + new String(b, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (din != null) {
din.close();
}
} catch (IOException e) {
}
}
}
}