2010-01-11 3 views
0

java에서 file.renameTo의 모든 기능을 원하지만 소스 파일을 삭제하지 않고 싶습니다. 예 : report.doc 파일이 있는데 보고서 파일을 삭제하지 않고 report.xml을 만들고 싶습니다. 또한 두 파일의 내용이 동일해야합니다. (간단한 복사본) 어떻게해야합니까?java의 같은 디렉토리에 파일의 복사본을 만드는 방법은 무엇입니까?

나는 이것이 사소한 것일 수도 있지만 기본적인 검색은 도움이되지 않는다는 것을 알고있다.

답변

1

는 원래의 동일한 내용으로 새 파일을 만들 수 있습니다.
는 그것을 위해 자바 NIO (자바 1.4 이상)를 사용

private static void copy(File source, File destination) throws IOException { 
    long length = source.length(); 
    FileChannel input = new FileInputStream(source).getChannel(); 
    try { 
     FileChannel output = new FileOutputStream(destination).getChannel(); 
     try { 
      for (long position = 0; position < length;) { 
       position += input.transferTo(position, length-position, output); 
      } 
     } finally { 
      output.close(); 
     } 
    } finally { 
     input.close(); 
    } 
} 

가이 question에 대한 답변을 참조 더

관련 문제