2015-10-06 52 views
0

我想創建一個java安裝程序,將文件從源文件(如放置文件的packpage)複製到Appdata文件夾,這可能嗎?我怎樣才能做到這一點?將文件從源文件複製到電腦

+0

[http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html](http://docs.oracle.com/javase/8/docs/api /java/nio/file/Files.html)。 – azurefrog

+0

http://stackoverflow.com/questions/3509562/how-to-make-installer-pack-of-java-swing-application-project –

+0

我會試試這個,坦克你 – TxCraft

回答

1
String homeDir = System.getProperty("user.home"); 
String myAppFolderName = ".MyApp"; 
Path installDir = Paths.get(homeDir, "AppData"); 
if (!Files.isDirectory(installDir) { // Maybe not Windows 
    installDir = Paths.get(homeDir); 
} 
Path myAppFolder = Paths.get(installDir.toString(), myAppFolderName); 
Files.createDirectory(myAppFolder); 

Path sources = Paths.get(new URI("jar:file://... .jar!/install_image")); 
Files.copy(sources, myAppFolder); 

對於一個罐子裏的文件,URI:

MyAppClass.class.getProtectionDomain() 
    .getCodeSource().getLocation().toURI().getPath() 

它使用

  • 一個回落時沒有AppData目錄(如在Linux或Mac)
  • 一些子目錄.MyApp將所有內容放入
  • 用於拆包的zip文件系統(「jar:file:/ ...」)
  • 一種方式來獲得一個罐子

你可能會想抓住罐子沒有運行過的情況下的URI - 進行開發。