2013-05-07 3 views
0

기존의 디렉토리 구조를 복사하려고합니다 (파일 내용 자체가 필요 없으며 0 개의 더미 파일이 필요합니다). 그러나 mkdirs()은 이 IOException 인 원인이되는 필요한 디렉토리를 생성하지 않습니다.java 파일 mkdirs 및 createNewFile이 작동하지 않습니다.

java.io.IOException: The system cannot find the path specified 
    at java.io.WinNTFileSystem.createFileExclusively(Native Method) 
    at java.io.File.createNewFile(Unknown Source) 

가 나는 또한

File origParentFile = fileToCopy.getParentFile(); 
File newParent = new File(origParentFile.getCanonicalPath().replace("O:\\", "C:\\xfer\\")); 
localVersion = new File(newParent, fileToCopy.getName()); 

을 시도,하지만 그 중 하나가 작동하지 않았다 :

private static void readAndCopy(File fileToCopy) throws IOException { 
    File localVersion = new File(fileToCopy.getCanonicalPath().replace("O:\\", "C:\\xfer\\")); 
    System.out.println("Replicating " + fileToCopy.getCanonicalPath() + " to " + localVersion.getCanonicalPath()); 

    if (fileToCopy.isDirectory()) { 
     boolean dirCreated = localVersion.getParentFile().mkdirs(); 
     System.out.println(localVersion.getCanonicalPath() + " " + (dirCreated ? "" : "not ") + "created"); 

     if (dirCreated) { 
      for (File content : fileToCopy.listFiles()) { 
       readAndCopy(content); 
      } 
     } 

    } else { 
     if (!localVersion.exists()) { 
      localVersion.createNewFile(); 
     } 
    } 
} 

public static void main(String[] args) throws IOException { 
    readAndCopy(new File("o:\\MY_SRC_DIR")); 
} 

오류 메시지는 다음과 같습니다 코드입니다.

+0

어떤 IOException이 발생합니까? 무슨 메시지 야? – EJP

+0

죄송합니다. @EJP, 깜빡했습니다. 업데이트 된 설명을 참조하십시오. –

답변

1

당신은 틀렸어. 'mkdirs() is creating all the directories including the file name itself as a directory. You need to call localVersion.getParentFile(). mkdirs().

+0

나는 그것을 시도했지만, 증상은 매우 똑같다 : 디렉토리, 서브 디렉토리가 생성되지 않았으며,'createNewFile()'는'IOException'을 던진다. 'MY_SRC_DIR'은 이미'C : \ xfer '아래에 존재합니다. –

+0

'mkdirs()'가 true를 반환하지만 아무것도하지 않는다는 것을 의미합니까? 디스크에 실제 디렉토리가 생성되지 않았습니까? – EJP

+0

아니요, mkdirs()는 상위 디렉토리가 이미 존재하더라도 (이전에 수동으로 생성 된 경우에도) false를 반환합니다. –