IO流的一个问题,关于汉字的输入和输出

csdnquestion 2013-06-13 10:24:19
最近在学习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。
...全文
88 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
冰思雨 2013-06-14
  • 打赏
  • 举报
回复
Scanner
cainiao000 2013-06-14
  • 打赏
  • 举报
回复
import java.io.InputStream;
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();
}


}

}
团团员圆 2013-06-13
  • 打赏
  • 举报
回复
public static void main(String[] args) { 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(); } } }

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧