如何复制excel个文件?

How to copy excel file?

我正在使用这种方法来复制文件。它适用于所有简单的文件类型,例如 csvtxtpdf 等。 . .除了 xlsx。我不知道为什么 Excel 文件不想被复制。它已损坏

public static void copyFileFromTo(File source, File dest)   {

    InputStream input = null;
    OutputStream output = null;

    try {
        input = new FileInputStream(source);
        output = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buf)) > 0) {
        output.write(buf, 0, bytesRead); 
        }  
        input.close();
        output.close();
    }

    catch (IOException e) { 
    JOptionPane.showMessageDialog(null, e.getMessage() ); 
    System.exit(-1); 
 } 

} 

Excel 文件实际上是 ZIP 文件(尝试在压缩程序中打开)——也许这就是问题所在。我不在 Java 中编码,但我建议寻找一个 ZIP 复制例程 - 这是我在 SO 上找到的一个例程:Best Way to copy a Zip File via Java

public final static int BUF_SIZE = 1024; //can be much bigger, see comment below


public static void copyFile(File in, File out) throws Exception {
  FileInputStream fis  = new FileInputStream(in);
  FileOutputStream fos = new FileOutputStream(out);
  try {
    byte[] buf = new byte[BUF_SIZE];
    int i = 0;
    while ((i = fis.read(buf)) != -1) {
        fos.write(buf, 0, i);
    }
  } 
  catch (Exception e) {
    throw e;
  }
  finally {
    if (fis != null) fis.close();
    if (fos != null) fos.close();
  }
}

因为这对你不起作用,试试这个:http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy%28java.nio.file.Path,%20java.nio.file.Path,%20java.nio.file.CopyOption...%29

import java.io.IOException;
import java.nio.file.*;

public class Program {
    public static void main(String[] args) {

        FileSystem system = FileSystems.getDefault();
        Path original = system.getPath("C:\programs\my.xlsx");
        Path target = system.getPath("C:\programs\my2.xlsx");

        try {
            // Throws an exception if the original file is not found.
            Files.copy(original, target, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException ex) {
            System.out.println("ERROR");
        }
    }
}

我在将更多字节复制到新文件之前遇到了这个问题:

----->如果原始尺寸为=1050;

-----> 新文件的大小为 = 2048;

试试这个:

    public static void copyFileFromTo(File source, File dest)   {

        InputStream input = null;
        OutputStream output = null;

        try {
            int buf_size=1024;
            input = new FileInputStream(source);
            output = new FileOutputStream(dest);
            System.out.println("Size of the file :"+input.available()+" Byte");
            byte[] buf = new byte[buf_size];
            int bytesRead;
            while ((bytesRead = input.read(buf)) > 0 ) {
            output.write(buf, 0, bytesRead);
            if(input.available()<buf_size){
                System.out.println("Availble byte now is : "+input.available()+" so change the size of the array byte the to the same size");
                //if the available byte are <1024  you will copy  a array of 1024 to the file that cause domage to file
                buf= new byte[input.available()];
            }
            }                               
            input.close();
            output.close();
        }

        catch (IOException e) { 
        JOptionPane.showMessageDialog(null, e.getMessage() ); 
        System.exit(-1); 
     } 

}