在JAVA中怎样用system.in.read()读入字符串存到String中

来源:百度知道 编辑:UC知道 时间:2024/05/11 05:57:30
如题

import java.io.*;

public class TestInput
{
public static void main(String args[])
{

InputStreamReader stdin = new InputStreamReader(System.in);//键盘输入
BufferedReader bufin = new BufferedReader(stdin);
try
{

String str = bufin.readLine();

}
catch(IOException E)
{
System.out.println("发生I/O错误!!!");
}
}
}

该方法输入是console;返回值是int;保存的是console中输入的第一个字符的ASCII值;通过enter进行flush;

你可以用while循环将返回值保存在StringBuffer里面;在后再调用toString();

我个人很少用这个方法来读取console中的输入;个人感觉用InputStream会好一点;

题在哪里啊 ??

public class Test{
public static void main(String []args)throws Exception{
byte[] b=new byte[1024];
System.out.print("请输入一个字符串:");
System.in.read(b);

String str=new String(b);

System.out.print(str);
}
}

你根据要输入的字符串的大小去设置byte数组的大小