个人觉得word文档应该是字符文本,但用字符流读取写入均出现乱码- public static void method1() throws FileNotFoundException, IOException {
- FileReader fr = new FileReader("f:\\io\\java.doc");
- FileWriter fw = new FileWriter("f:\\io\\javacopy.doc");
- BufferedReader bufr = new BufferedReader(fr);
- BufferedWriter bufw = new BufferedWriter(fw);
- String line = null;
- while((line = bufr.readLine())!=null){
- bufw.write(line);
- bufw.newLine();
- bufw.flush();
- }
- fw.close();
- fr.close();
- }
复制代码 拷贝出现乱码,而且比源文件小- public static void method2() throws IOException {
- BufferedReader bufr = new BufferedReader(new InputStreamReader(new FileInputStream("f:\\io\\java.doc")));
- //BufferedWriter bufw = new BufferedWriter(new FileWriter("f:\\io\\javacopy.doc")); //与下面一样乱码
- BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("f:\\io\\javacopy.doc")));
- String line = null;
- while((line = bufr.readLine())!=null){
- bufw.write(line);
- bufw.newLine();
- bufw.flush();
- }
- bufw.close();
- bufr.close();
- }
复制代码 依然是乱码,并且比源文件小- public static void method3() throws IOException {
- BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("f:\\io\\java.doc"));
- BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("f:\\io\\javacopy.doc"));
- int ch = 0;
- while((ch=bufis.read())!=-1){
- bufos.write(ch);
- }
- bufos.close();
- bufis.close();
- }
复制代码 用字节流拷贝正常
world文档里面都是字符文本吗,为什么用字符流读取写入会出现问题,用转换流也出现问题,只能用字节流,还是world文档有特殊的编码,需要转码?
|
|