2014-11-24 5 views
0

내 Ressources에 폴더가 있습니다. 앱을 처음 시작할 때 디스크에 추출하려고합니다. 여기에 내가 디스크에 파일을 복사하려고했던 코드의 평화가 있습니다.하지만 빈 파일 만 있으면됩니다. 폴더에는 .gnh 파일이 들어 있습니다. 파일의 바이트를 잃어 버리는 곳은 어디입니까? 나는 간단한 텍스트 파일 모든 것이 제대로 작동 복사 할 때 :자바 저장 리소스 폴더 디스크 폴더

public void getTemplates() throws URISyntaxException { 
    final URL url = TemplateUtils.class.getResource("/templates/"); 
    if (url != null) { 
     final File dir = new File(url.toURI()); 
     for (final File file : dir.listFiles()) { 
      try { 


       final OutputStream outStream = new FileOutputStream(
         PathManager.INSTANCE.getRootPath() + file.getName()); 

       final long writtenBytes = Files.copy(file.toPath(), outStream); 
       LOG.info(writtenBytes); 
       outStream.flush(); 
       outStream.close(); 


      } catch (final IOException e) { 
       LOG.error(e.getMessage(), e); 
      } 
     } 
    } 
} 

LOG.info (writtenBytes는) 0

편집 말했다. 하지만 그 .gnh 파일로는 더 이상 아무것도 작동하지 않습니다. 디스크에 파일을 추출하는 다른 방법이 있습니까?

+0

정확히 fileInfo에서 무엇을 의미합니까? 나는이 시점에서 file.length()를 기록했고 그것은 0이었다. 그래서 간단한 텍스트 파일을 시도하고 내 메서드가 작동했습니다. 문제는이 바보 같은 .gnh FileFormat입니다. 폴더를 복사/추출하는 또 다른 방법이 있습니까? – Tobi

+0

잘못된 설명입니다. 나는 오해했다. 그것을 삭제했다. –

답변

0

나는 해결책을 얻었습니다 : 먼저 OutputStream에 대한 파일을 생성 한 다음 플러시 할 수 있어야합니다.

final File path = new File(
      PathManager.INSTANCE.getRootPath() + "templates"); 
    path.mkdirs(); 
     final File newFile = new File(path.toString() 
       + File.separator + file.getName()); 
    newFile.createNewFile(); 
    final OutputStream outStream = new FileOutputStream(newFile); 
    Files.copy(file.toPath(), outStream); 
    outStream.flush(); 
    outStream.close();