要用到OutputStreamWrite。因为OutputStreamWriter 将字节流转换为字符流。是字节流通向字符流的桥梁。如果不指定字符集编码,该解码过程将使用平台默认的字符编码,如:GBK。
完整代码:
import java.io.*;
class TransStreamDemo2
{
public static void main(String[] args)throws IOException
{
//键盘录入
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//存入文件中,按照指定的编码表(UTF-8)
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("readin1.txt"),"UTF-8"));
String line=null;
while((line=br.readLine())!=null)
{
if("over".equals(line))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
}
} |