File inlog = new File("D:/Water lilies.jpg");
     File charFile = new File("D:/Water lilies backup.jpg");
     if(charFile.exists()) charFile.delete(); 
     charFile.createNewFile();
     FileReader fr = new FileReader(inlog);
     FileWriter fw = new FileWriter(charFile);
     BufferedReader br = new BufferedReader(fr);
     BufferedWriter bw = new BufferedWriter(fw);
     int bbb = fr.read();
     while(bbb != -1){
      fw.write(bbb);
      bbb = fr.read();
     }
     fw.flush();为什么复制出来的图片,上面全都出现了很多红点点呢?

解决方案 »

  1.   

    图片是二进制文件,不能使用 Reader/Writer 来进行读写,必须使用二进制流 InputStream/OutputStream 来读写。
      

  2.   


    static final int BUFF_SIZE = 100000;
    static final byte[] buffer = new byte[BUFF_SIZE];

    public static void main(String[] args) throws IOException {

       InputStream in = null;
       OutputStream out = null; 
       try {
          in = new FileInputStream("D:/1.jpg");
          out = new FileOutputStream("D:/2.jpg");
          while (true) {
             synchronized (buffer) {
                int amountRead = in.read(buffer);
                if (amountRead == -1) {
                   break;
                }
                out.write(buffer, 0, amountRead); 
             }
          } 
       } finally {
          if (in != null) {
             in.close();
          }
          if (out != null) {
             out.close();
          }
       }
      

  3.   

    我们使用 NIO 中的 FileChannel 也能实现文件复制,下面的代码中提供了两种复制方法:一种是 NIO,另一种是使用传统的 IO:import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.Closeable;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.channels.FileChannel;public class FileCopy {    public static void main(String[] args) throws IOException {
            long result = ioCopy("e:/灰太狼.gif", "e:/灰太狼_bak.gif");
            System.out.println(result);
        }    /**
         * NIO 文件复制
         * @param srcName
         * @param destName
         * @return  复制的字节数量
         * @throws IOException
         * @author frankiegao123
         * 2010-4-29 下午01:57:01
         */
        public static long nioCopy(String srcName, String destName) throws IOException {
            return nioCopy(new File(srcName), new File(destName));
        }    public static long nioCopy(File src, File dest) throws IOException {
            FileChannel srcChannel = null;
            FileChannel destChannel = null;
            long result = -1;
            try {
                srcChannel = new FileInputStream(src).getChannel();
                destChannel = new FileOutputStream(dest).getChannel();
                result = srcChannel.transferTo(0, src.length(), destChannel);
            } finally {
                close(srcChannel);
                close(destChannel);
            }
            return result;
        }    /**
         * 传统 IO 文件复制
         * @param srcName
         * @param destName
         * @return  复制的字节数量
         * @throws IOException
         * @author frankiegao123
         * 2010-4-29 下午01:57:08
         */
        public static long ioCopy(String srcName, String destName) throws IOException {
            return ioCopy(new File(srcName), new File(destName));
        }    public static long ioCopy(File src, File dest) throws IOException {
            BufferedInputStream in = null;
            BufferedOutputStream out = null;
            long result = -1;
            try {
                in = new BufferedInputStream(new FileInputStream(src));
                out = new BufferedOutputStream(new FileOutputStream(dest));
                byte[] bys = new byte[8 * 1024];
                long tmpResult = 0;
                for (int n = -1; (n = in.read(bys)) > -1; tmpResult += n) {
                    out.write(bys, 0, n);
                }
                result = tmpResult;
            } finally {
                close(in);
                close(out);
            }
            return result;
        }    private static void close(Closeable io) {
            if (io != null) {
                try {
                    io.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }