用java io读取一个文件,文件格式可能是txt,excel或者其他的。比如一个xls后缀的excel文件  路径:c:\test\test.xls用java io 读取这个文件后,要把它完整的(包括格式)备份到 c:\test\bkr 目录下面。应该怎么实现?谢谢

解决方案 »

  1.   

    如果原来的文件不要了,就file1.renameTo(file2)
    如果要,就读出来,写入新文件中
      

  2.   

    不行啊,我刚才试过了,读取了一个excel文件,然后把它写入另一个excel文件,结果写一行中去了,
    而且还出现了乱码。
      

  3.   

    用FileInputStream、FileOutputStream读写byte数组就可以了
      

  4.   

    public static int copyFile(String src, String dst) {
        try {
          int len = 0;
          byte[] buf = new byte[1024];
          FileInputStream fis = new FileInputStream(src);
          FileOutputStream fos = new FileOutputStream(dst);
          while ( (len = fis.read(buf)) != -1) {
            fos.write(buf, 0, len);
          }
          fis.close();
          fos.close();
        }
        catch (IOException e) {
          System.out.println(e);
          return -1;
        }
        return 0;
      }例子:
    copyFile("c:\\test\\test.xls","c:\\test\\bkr\\test.xls");