键盘控制输入字符,并且有转换流(InputStreamReader)加入,同样也是使用了缓冲。

package com.bjsxt.ioproject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException; public class TestSystemin {
public static void main(String[] args) {
BufferedReader br=null;
BufferedWriter bw=null;
try {
InputStream is=System.in;
System.out.println("请输入内容:");
InputStreamReader isr=new InputStreamReader(is,"gbk");
br = new BufferedReader(isr);
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("F:\\.......\\资料\\yxf.txt"),true),"gbk"));
String len=null;
while (!"over".equals((len=br.readLine()))) {
bw.write(len);
bw.newLine();
bw.flush();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if (bw!=null) {
bw.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (br!=null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
}

运行前:

Java修炼——键盘输入,字节转换流-LMLPHP

运行结果:

Java修炼——键盘输入,字节转换流-LMLPHP

05-11 15:19