代码如下:
package test;
import java.io.File;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;class DataIODemo {    boolean flag = false ;
    public boolean copy(File f1,File f2)
    {
        FileInputStream fis = null;
        DataInputStream dis = null;
        DataOutputStream dos = null;
        FileOutputStream fos = null;        try {
            fis = new FileInputStream(f1);
            fos = new FileOutputStream(f2);
            dis = new DataInputStream(fis);
            dos = new DataOutputStream(fos);
            int temp;            while((temp=dis.read())!=-1)
            {
                dos.write(temp);////////////////////是不是这里不妥?
            }
            flag = true;        } catch (Exception e) {
            System.out.println(e.toString());
            flag =false;
        } finally {
            try {
                dis.close();
                fis.close();
                dos.close();
                fos.close();            } catch (Exception e) {
                System.out.println(e.toString());
                flag = false;
            }
        }
        return flag;
    }
    public static void main(String[] args) {
        DataIODemo demo = new DataIODemo();
       if( demo.copy(new File("e:\\1.jpg"),new File("e:\\2.jpg")))
       {
           System.out.println("复制成功");
       }
       else
       {
           System.out.println("复制失败");
       }
    }
}///在上面我注释的地方,是不是不妥?一个字符一个字符读写(dis.read()),影响了性能吗?
////如果程序还存在其他不错的地方。请指教

解决方案 »

  1.   

    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
      

  2.   

    建议看看Ant的FileUtils的copy功能
      

  3.   

    每次读一个字节,当然慢了,每次先读到一个数组里面,数组长度大点, 大概数组容量是 1K的时候速度跟windows拷贝差不多了
      

  4.   

    fis = new FileInputStream(f1);
                fos = new FileOutputStream(f2);
                dis = new DataInputStream(fis);
                dos = new DataOutputStream(fos);
                int temp;            while((temp=dis.read())!=-1)
                {
                    dos.write(temp);////////////////////是不是这里不妥?
                }------------------------------------------------------
    这样读当然慢了,最好先读到一个buffer里byte buffer = new byte [1024];
    ……
    while(dis.read(buffer)!=-1)
    {
       dos.write(buffer);////////////////////是不是这里不妥?
    }