我修改了下原来的代码:
fos.write(b, 0, len); 然后每次只读一个字节,奇怪的是,p.txt竟然汉字正确显示,控制台依然乱码,求解释。
- package day20;
- import java.io.*;
- public class FileStreamDemo {
- public static void main(String[] args) throws IOException,
- InterruptedException {
- File f = new File("prop.txt");
- FileInputStream fis = new FileInputStream(f);
- // OutputStreamWriter fos = new OutputStreamWriter(new
- // FileOutputStream("p.txt"),"UTF-8");
- FileOutputStream fos = new FileOutputStream("p.txt");
-
- byte[] b = new byte[1];
- int len;
- while ((len = fis.read(b)) != -1) {
- System.out.print(len);
- String s = new String(b, 0, len);
- System.out.print(s);
- // Thread.sleep(5000);
- fos.write(b, 0, len);
- fos.flush();
- }
- fis.close();
- fos.close();
- }
- }
复制代码 控制台:
- 1#1
- 1
- 1#1W1e1d1 1F1e1b1 12151 11161:13141:13171 1C1S1T1 121011151
- 1
- 1?1?1?1?1=1?1?1?1?1
- 1
- 1?1?1?1?1?1?1=1?1?1?1?1?1?1?1?1?1?1
- 1
- 1t1i1m1e1=151
- 1
- 1?1?1?1?1?1?1=1?1?1?1?1?1?1?1?1?1?1?1?1?1?1
- 1
复制代码 p.txt如下:
- #
- #Wed Feb 25 16:34:37 CST 2015
- 李朝=黑马
- 马士兵=北京尚学堂
- time=5
- 毕向东=黑马程序员导师
复制代码
|