LZ这样试试: PrintStream console = System.out; String s = new String(); ByteArrayOutputStream f= new ByteArrayOutputStream(); PrintStream out = new PrintStream(f); System.setOut(out); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); bw.write("hello word"); bw.flush(); s=f.f.toString();
StringWriter sw = new StringWriter();
BufferedWriter bw = new BufferedWriter(sw);
try {
bw.write("HelloWorld");
bw.flush();
// 写更多的字符串进去,这样所有字符串都写进sw中了,然后
} catch (IOException e) {
e.printStackTrace();
}
String str = sw.toString(); // 得到bw中写的字符串这是我实现的代码,不知道是否符合要求。
是用标准输出流构造的。如上!呵呵,大鸟们救命啊。
你这样写的话,已经写到了标准输出了
bw本身里面是没有这个数据的
所以,你无法再写入string
一边写进去,一边读进来,读出来的再写入String变量呵呵,不过不知道这么做有什么意义
直接写入String变量,然后再输出到标准输出不就行了?
关键在于,你到底想实现怎样的功能
PrintStream console = System.out;
String s = new String();
ByteArrayOutputStream f= new ByteArrayOutputStream();
PrintStream out = new PrintStream(f);
System.setOut(out);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
bw.write("hello word");
bw.flush();
s=f.f.toString();
System.out已经是输出流了,只能往里边写东西,需要获取键盘输入流可以使用System.in