最近在学习IO流,遇到一个难题,看下面的代码:public static void main(String[] args) throws IOException {
InputStream in=System.in;
int ch=in.read();
System.out.println((char)ch);
}在控制台输入字符a,可以在输出a。但是如果想输入一个汉字,如何输出一个汉字呢?有个要求,就是实现时不能使用InputStreamReader和OutputStreamWriter。
InputStream in=System.in;
int ch=in.read();
System.out.println((char)ch);
}在控制台输入字符a,可以在输出a。但是如果想输入一个汉字,如何输出一个汉字呢?有个要求,就是实现时不能使用InputStreamReader和OutputStreamWriter。
InputStream in = System.in;
byte[] b = new byte[1024];
int len = -1;
try {
while((len = in.read(b)) != -1){
System.out.println(new String(b,0,len));
break;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.IOException;
import java.lang.String;public class test0011 { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream in=System.in;
try {
byte b[] = new byte[1024]; int len = 0;
int temp=0; //所有读取的内容都使用temp接收
while((temp=in.read())!=-1){ //当没有读取完时,继续读取
b[len]=(byte)temp;
len++;
System.out.println(new String(b,0,len)); }
// System.out.println(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}