2017-10-29 3 views
1

한 폴더에 파일을 나열한 다음 다른 폴더로 파일을 전송해야했습니다. 그러나, 내가 얻는 문제는 다음과 같습니다 ... 다른 폴더에 붙여 넣으려고 할 때 경로가 E : \ Files와 같아서 어떤 종류의 파일을 생성하게하고 그 파일을 나와 같이 붙이지 않아야합니다. 나는 그것을 할 수 없습니다 여전히 여러 가지 방법을 시도하고, 당신이 나에게다른 디렉터리에 파일을 복사하는 방법

Path algo = Paths.get("E:/Files/"); 
    public void Copy(String origenArchivo, Path algo) { 
       Path origenPath = Paths.get(origenArchivo); 
       String s = algo.toAbsolutePath().toString(); 
       System.out.println(s); 
       Path destinoPath = Paths.get(s); 
       System.out.println(destinoPath); 
       String x = destinoPath.toString() + "/"; 

       Path conv = Paths.get(x); 
       System.out.println(conv); 
     try { 

      Files.copy(origenPath, conv, StandardCopyOption.REPLACE_EXISTING); 
       } catch (IOException ex) { 
      Logger.getLogger(Metodos.class.getName()).log(Level.SEVERE, null, ex); 
     } 



    } 




File dir = new File("C:/Users/PC/Desktop/"); 
     public void TravelToFantasy(File dir) { 
     File listFile[] = dir.listFiles(); 
     if (listFile != null) { 
      for (int i = 0; i < listFile.length; i++) { 
       if (listFile[i].isDirectory()) { 
        Copy(listFile[i]); 
       } else { 
        System.out.println(listFile[i].getPath()); 
        System.out.println(destino); 
        this.Copy(listFile[i].getPath(), algo); 

       } 
      } 
     } 
     } 

나는 "/"Paths.get 저를 얻는 경로에 넣어려고 도움을 줄 수 있는지 확인하기 위해 내 코드를 떠나지 만, 항상 내가 E : \ Files 경로를 남기게된다.

고마워 네!

답변

1

Files.copy에 디렉터리를 전달할 수 없습니다.

문자열과의 모든 변환이 필요하지 않습니다. 그냥 대신 Path.resolve를 사용

public void copy(String origenArchivo, Path algo) 
throws IOException { 
    Path origenPath = Paths.get(origenArchivo); 
    Path conv = algo.resolve(origenPath.getFileName()); 
    Files.copy(origenPath, conv, StandardCopyOption.REPLACE_EXISTING); 
} 

를 보조 노트, 작업이 실패 할 경우 그들이 의미있는 정보를 제공하기 때문에 경로/경로/파일 클래스는 java.io.File의 클래스에 우수으로. 당신은 모든 java.io.File에 사용하지 말아야 : 없음 작동

Path dir = Paths.get("C:/Users/PC/Desktop/"); 

public void TravelToFantasy(Path dir) 
throws IOException { 
    try (DirectoryStream<Path> listFile = Files.newDirectoryStream(dir)) { 
     for (Path file : listFile) { 
      if (Files.isDirectory(file)) { 
       Copy(file); 
      } else { 
       System.out.println(file); 
       System.out.println(destino); 
       this.Copy(file, algo); 
      } 
     } 
    } 
} 
+0

이것은 작동합니다! 많은 VGR 감사, 나는 이해하고 모든 것이 올바르게 작동합니다. – bukkevimli

0

"/"대신 java.io.File.separator를 사용하면 코드가 모든 OS를 실행하는 데 도움이됩니다.

+0

... 난이 추가 : 문자열 X = destinoPath.toString() + Win32 시스템을; 그러나 이것은 작동하지 않습니다 – bukkevimli

관련 문제