2016-07-17 7 views
1

이 방법을 사용하여 파일을 복사하고 있습니다. csv, txt, pdf 등 모든 간단한 파일 형식에 완벽하게 작동합니다. . . 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); 
} 

} 

답변

2

Excel 파일은 실제로 압축 파일로 열립니다. 압축 파일을 열어보십시오. 아마도 문제 일 수 있습니다. 나는 자바 코드를하지 않습니다하지만 난 ZIP 복사 루틴을 찾는 게 좋을 것 - 여기에서 내가 SO 여기에서 찾을 수있다 : 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

:이 시도에 대한 그 이후 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(); 
    } 
} 

작동하지 않았다

+0

감사합니다. 나는 엑셀이 지퍼라는 것을 몰랐다. +1 :)하지만 당신이 위에 게시 한 방법은 내가 내 질문에 게시 한 것과 비슷합니다. 그것도 작동하지 중 어느 쪽이든 – john

+0

@ 존 그들은 내부에 XML 파일과 ZIP 아카이브입니다. 내 업데이트 된 게시물을 참조하십시오. 아마 두 번째 코드 세그먼트가 도움이 될 것입니다. – Matt

+0

그 방법은 Java 7 이상을 사용합니다. woudl이 작동하지 않는 java 6 용 타겟으로 컴파일 할 때. 많은 기계에는 아직도 자바 6이있다. 내도 too – john

1

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

} 
+0

작동 여부 확인 – Cherif

관련 문제