2017-03-03 54 views
0

我想從我的.jar文件存在的位置複製目錄? 這裏是我嘗試..但我總是得到/家/用戶/ 我怎麼能從我的.jar程序存在的地方複製文件?在java中使用.jar程序複製目錄

private void copy_dir() { 

    //Path sourceParentFolder = Paths.get(System.getProperty("user.dir") +  "/Project/"); 
    //  Path sourceParentFolder = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString()); 
    Path destinationParentFolder = Paths.get(System.getProperty("user.home")); 

    try { 
     Stream<Path> allFilesPathStream = Files.walk(sourceParentFolder); 
     Consumer<? super Path> action = new Consumer<Path>() { 

      @Override 
      public void accept(Path t) { 
       try { 
        String destinationPath = t.toString().replaceAll(sourceParentFolder.toString(), destinationParentFolder.toString()); 
        Files.copy(t, Paths.get(destinationPath)); 
       } catch (FileAlreadyExistsException e) { 
        //TODO do acc to business needs 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 

     }; 
     allFilesPathStream.forEach(action); 

    } catch (FileAlreadyExistsException e) { 
     //file already exists and unable to copy 
    } catch (IOException e) { 
     //permission issue 
     e.printStackTrace(); 
    } 

} 
+0

一些可以理解的英語怎麼樣?什麼是錯誤? – XtremeBaumer

+1

爲什麼你得到'user.home'的路徑?你想要的是當前目錄('.')。 – m0skit0

+0

user.home是目的地或目標.. –

回答

1

嘗試

Path destinationParentFolder = Paths.get(System.getProperty("user.dir")); 

「user.dir來」會從您的應用程序初始化的絕對路徑。

「user.home」獲取用戶的主目錄。

+0

這不起作用..它給我/家/用戶 –