randFile.write(resultBs, start, byteLen);
应该是read方法吧??

解决方案 »

  1.   

    用RandomAccessFile生成的文件是文本文件
    文件名后缀是rar
    默认使用压缩软件打开
    当然会出错
      

  2.   

    写的有点混了,不知道楼主是想用RandomAccessFile还是OutputStream把文件写入磁盘
    改了下,可以参考下
    public static void main(String args[]) throws IOException {
    String filePath = "D:\\aa.rar";
    String dest="D:" + File.separator + "testfile.jar";
    int flag=copyFileToDest(filePath,dest);
    System.out.println("文件复制"+(flag==1?"成功":"失败")); }

    public static int copyFileToDest(String src,String dest){
    int flag=1;
    File files = new File(src);
    int start = 0;
    int byteLen = (int) files.length();
    byte[] resultBs = new byte[byteLen];
    RandomAccessFile randFile = null;
    try {
    InputStream ins = new FileInputStream(files);
    ins.read(resultBs, start, byteLen);
    randFile = new RandomAccessFile(dest, "rw");
    randFile.write(resultBs, start, byteLen);
    ins.close();
    randFile.close();
    } catch (Exception e) {
    flag=0;
    e.printStackTrace();

    }
    return flag; }