最近在学习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。IO
InputStream in=System.in;
int ch=in.read();
System.out.println((char)ch);
}在控制台输入字符a,可以在输出a。但是如果想输入一个汉字,如何输出一个汉字呢?有个要求,就是实现时不能使用InputStreamReader和OutputStreamWriter。IO
解决方案 »
- 请教各位达人一道JAVA基础题,并附上自己的思路,望大家给予指点
- 如何实现窗口的隐藏
- 哪位大哥,给我改一下程序啊!我弄不出来了哈!谢谢!
- import 导入其他盘的自己写的class文件,如何操作?
- Java的HTML parser哪一个比较好用?
- 关于科学计数法转换的问题
- jbuilder 的配置问题,我们有一个需要导入的库文件的配置文件 ***.library,别人拷过去都能用,我在我的机器上是jbuiler状态栏提示java.io
- 一个程序,在jubilder环境里面可以运行,出来就不行了,报下面的错误。
- 请问Java可否调用MsSQL的存储过程,如何调用?
- 求教关于范围的问题?
- Java中Calendar日历的一点疑问
- Java如何将字符串放进字符串数组中
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));
}
} catch (IOException e) {
e.printStackTrace();
}
}
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();
}
}
}
你只需要吧这些byte存起来即可。下面是示例代码。
,import java.io.*;
import java.util.*;
public class TestInput
{
public static void main(String[] args) throws Exception
{
InputStream in=System.in;
int ch = 0;
List<Byte> buff = new ArrayList<Byte>();
//这里用13 是为了实现用一个回车就读buffer 然后结束的效果
//正常情况下是返回-1 (ctrl+c)
while ((ch = in.read())!=13)
{
buff.add((byte)ch);
}
//toArray只适用于Object Byte 类型
byte[] buff2 = new byte[buff.size()];
for (int i = 0;i<buff2.length ;i++ )
{
buff2[i] = buff.get(i);
}
System.out.println(new String(buff2,"gbk"));
}
}
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();
}
}}