我想用 java 创建一个安装程序,将文件从源(如放置文件的包页)复制到 Appdata 文件夹,这可能吗?我怎样才能做到这一点?
最佳答案
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);
对于 jar 的文件,URI:
MyAppClass.class.getProtectionDomain()
.getCodeSource().getLocation().toURI().getPath()
这使用
- 只要没有 AppData 目录(如在 Linux 或 Mac 上),就会回退
- 一些子目录.MyApp,用于放置所有内容
- 用于解压的 zip 文件系统(“jar:file:/...”)
- 获取 jar URI 的方法
您可能也想捕获不使用 jar 运行的情况 - 用于开发。
关于java - 将文件从源复制到计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32972826/