如何将System.in读取的流用String代替?这样写对吗?
String s = "test\n";
InputStream ss = new StringBufferInputStream(s);
上面这句在Eclipse里会出现不建议使用的方法,我要如何修改呢?
谢谢。
String s = "test\n";
InputStream ss = new StringBufferInputStream(s);
上面这句在Eclipse里会出现不建议使用的方法,我要如何修改呢?
谢谢。
= new BufferedReader(new InputStreamReader(System.in));
= new BufferedReader(new InputStreamReader(System.in));
try {
String s = in.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
就是将System.in所取得的数据输入改成别的方式取得数据,
比如原来要从键盘输入:1234,现在要将这个输入流固定为String s = "1234",不从键盘取得数据,而是从一个字符串变量取得。但要将s转换成InputStream有格式。这个流将用于telnet的客房端程序。
但是要如何转换呢?
String sFirst;
java.io.BufferedReader in = new java.io.BufferedReader( new java.io.InputStreamReader(System.in));
System.out.print("Please input :");
sFirst = in.readLine();
System.out.print("You input is :"+sFirst );
}catch(Exception e){}
ByteArrayInputStream in = new ByteArrayInputStream( s.getBytes() );
所以如果要将String转成InputStream只能用
ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());