方法一:使用InputStramReader 和OutputStreamWriter
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class BufferedKey {
public void output()
{
BufferedReader bufr =
new BufferedReader (new InputStreamReader (System.in) );
/**
* 输入顺序 键盘-->内存 键盘是字节流,如果要取得字符流就需要转换
*/
BufferedWriter bufw =
new BufferedWriter (new OutputStreamWriter (System.out));
/**
* 输出顺序 内存-->屏幕 屏幕输出的是字节流,所以将内存中字符流转换成字节流,然后在转化成字符流输出
*/
/**
* 上面两个转换是否重复了,我们输入直接读取字节流,然后让字节流直接放进OutputStreamWriter?
*/
String line;
try
{
while ( (line = bufr.readLine() ) != null )
{
bufw.write(line);
bufw.newLine();
bufw.flush();
//bufw.newLine(); 注意如果这条语句写在此处,换行是不正确的,bufw的数据要在“flush”后才会显示
//空行会显示在 bufw.write(line)的前面。儿不是我们想要的bufw.newline()后面
}
}
catch (IOException e) {
System.out.println("读写错误!");
e.printStackTrace();
}
}
public static void main (String argc[])
{
BufferedKey bk = new BufferedKey();
bk.output();
}
}
方法二:使用DataInputStream 和DataOutputStream
import java.io.*;
public class test {
public void demo () throws IOException{
DataInputStream in = new DataInputStream(System.in);
DataOutputStream out = new DataOutputStream (System.out);
int len =0;
byte[] ch = new byte[1024];
while( (len = in.read(ch))!= -1)
{
out.write(ch, 0, len);
}
}
public static void main(String[] args) throws IOException {
test t = new test();
t.demo();
}
}