import java.io.*;
public class ReadFile {

public static void main(String args[]){
ReadFile rf=new ReadFile();
try{
rf.copy("d:\\fusion32.rar","d:\\32.rar");
}
catch(Exception e){
e.printStackTrace();
}
}
public void copy(String fileName,String copyFileName) throws Exception{ BufferedReader reader=new BufferedReader(new FileReader(fileName));
    BufferedWriter writer=new BufferedWriter(new FileWriter(copyFileName));
    
    String str=null;
     str=reader.readLine();
    while(str!=null)
    {
     writer.write(str+"\r\n");
     
     str=reader.readLine();
    }
    reader.close();
    writer.close(); 
}

}拷贝的rar文件不能打开,长度有问题,请问是怎么回事?
如果有FileInputStream,FileOutputStream就没有问题。

解决方案 »

  1.   

    你用的是字符流,当然要出错了噻!!!
    rar是二进制的噻!
    所以说要用字节流!
      

  2.   


    rar是二进制的
    要用字节流
      

  3.   

    正好有段现成的===============
    public static void copyFile(File source, File dest){
    byte [] d = new byte[1024];
    try{
    FileInputStream fin = new FileInputStream(source);
    FileOutputStream fout = new FileOutputStream(dest);
    while(fin.read(d)!=-1){
    fout.write(d);
    }
    fout.close();
    fin.close();
    }
    catch(Exception e){
    e.printStackTrace();
    }
    }
      

  4.   

    To sandxu (阳光)        你的程序之所以会错,是因为你是用BufferedReader来读的,其实用它来读也没有错,错的是你不能用它的readLine方法,因为这个方法返回的是一个字符串,但是对于二制制流格式的rar文件来说,这样当然会出错的,你可以用它的read方法,把byte数组读到一个缓冲的数组里面,然后再用二制流的形式把它写出,这样就可以保证不出错了
    因为所有的文件都可以看作是字节的组合,文本文件也不例外