黑马程序员技术交流社区

标题: 文件拷贝乱码 [打印本页]

作者: Emperors    时间: 2014-4-12 10:10
标题: 文件拷贝乱码
本帖最后由 Emperors 于 2014-4-13 12:42 编辑

代码:
H:/d.png
乱码情况:

H:/l.png

d.png (12.84 KB, 下载次数: 46)

d.png

l.png (23.51 KB, 下载次数: 49)

l.png

作者: H._张_♂    时间: 2014-4-12 11:31
首先,楼主,你这是拷贝图片文件

public class FileReaderextends InputStreamReader用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。要自己指定这些值,可以先在 FileInputStream 上构造一个 InputStreamReader。

FileReader 用于读取字符流。要读取原始字节流,请考虑使用 FileInputStream。
public int read(char[] cbuf,
                int off,
                int len)
         throws IOException将字符读入数组的某一部分。
此方法实现 Reader 类相应 read 方法的常规协定。另一个便捷之处在于,它将通过重复地调用底层流的 read 方法,尝试读取尽可能多的字符。这种迭代的 read 会一直继续下去,直到满足以下条件之一:

已经读取了指定的字符数,
底层流的 read 方法返回 -1,指示文件末尾(end-of-file),或者
底层流的 ready 方法返回 false,指示将阻塞后续的输入请求。
如果第一次对底层流调用 read 返回 -1(指示文件末尾),则此方法返回 -1。否则此方法返回实际读取的字符数。
  1. package com.itheima;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;

  7. public class TestCopy {

  8.         public static void main(String[] args) throws IOException {
  9.                 // TODO 自动生成的方法存根
  10.                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("H:/d.png"));
  11.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("H:/l.bmp"));
  12.                 byte[] buff = new byte[1024];
  13.                 while(bis.read(buff)!=-1){
  14.                         bos.write(buff);
  15.                         bos.flush();
  16.                 }
  17.                 bis.close();
  18.                 bos.close();
  19.         }

  20. }
复制代码


作者: 钟成军    时间: 2014-4-12 11:58
本帖最后由 钟成军 于 2014-4-12 12:00 编辑

楼主你复制图片,只能用字节流,四个相关的类是
BufferedInputStream,BufferedOutputStream,FileInputStream,OutputStream
  1. import java.io.*;
  2. class CopyPic
  3. {
  4. public static void main(String[] args)
  5. {
  6. BufferedInputStream in = null;
  7. BufferedOutputStream out = null;
  8. File src = new File("c:\\1.png");
  9. File dir = new File("d:\\2.png");
  10. try
  11. {
  12. in = new BufferedInputStream(new FileInputStream(src));
  13. out = new BufferedOutputStream(new FileOutputStream(dir));

  14. byte[] buf = new byte[1024];
  15. int len = 0;
  16. while((len = in.read(buf))!=-1)
  17. {
  18. out.write(buf,0,len);
  19. out.flush();
  20. }
  21. }
  22. catch (IOException e)
  23. {
  24. throw new RuntimeException("复制失败");
  25. }
  26. finally
  27. {
  28. try
  29. {
  30. if(in!=null)
  31. in.close();
  32. }
  33. catch (IOException e)
  34. {
  35. throw new RuntimeException("读取失败");
  36. }
  37. try
  38. {
  39. if(out!=null)
  40. out.close();
  41. }
  42. catch (IOException e)
  43. {
  44. throw new RuntimeException("写入失败");
  45. }

  46. }
  47. }
  48. }
复制代码




作者: Emperors    时间: 2014-4-12 17:23
我是拷贝文本文件:L
作者: 钟成军    时间: 2014-4-12 18:19
钟成军 发表于 2014-4-12 11:58
楼主你复制图片,只能用字节流,四个相关的类是
BufferedInputStream,BufferedOutputStream,FileInputStrea ...

好吧,那你那两个png是啥子?
作者: H._张_♂    时间: 2014-4-13 11:28
楼主你被逗我了,png的文本文件几个意思?




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2