How to copy excel file?

这一生的挚爱 提交于 2021-01-28 23:31:37

问题


I'm using this method to copy files . It works perfectly for all simple file types such as csv , txt , pdf etc . . . except xlsx. I don't know why Excel file doesn't want to be copied . It gets corrupted

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); 
 } 

} 

回答1:


Excel files are actually ZIP files (try opening in a compression program) - maybe that's the issue. I don't code in Java but I'd suggest looking for a ZIP copying routine - here's one I found on SO here: 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();
  }
}

Since that didn't work for you try this: 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");
        }
    }
}



回答2:


i had this problem before you are copying more byte to new file more than the original size :

-----> if the original size is = 1050;

-----> the size of the new file is = 2048;

try this :

    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); 
     } 

} 


来源:https://stackoverflow.com/questions/38418146/how-to-copy-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!