允许用户将其"user"目录输入.jar安装程序


public static void moveSQLite(){
        File source = new File("C:\Users\520\Desktop\System.Data.SQLite"); // Specify initial path
        File target = new File("C:\Windows\Microsoft.NET\assembly\GAC_64"); // Desired path
        // If the source location exists, delete the any old source files
        // and copy the entire directory of the source to the target location
        if(source.exists()){
            System.out.println("Installing SQLite Database.");
            try {
                FileUtils.deleteDirectory(target);
                System.out.println("Deleting previous versions of System.Data.SQLite");
                System.out.println(""" + source + """ + " was successfully"
                        + " copied to " + """ + target + """);
                FileUtils.copyDirectory(source, target);
                System.out.println("SQLite database has been installed.");
            }catch (IOException e) {
                e.printStackTrace();
            }
        // Otherwise prompt the user that the source directory does not exist
        }else{
            System.out.println("SQLite not found - are you sure you have it in the right directory?");
        }
    }

以上是我的安装程序中使用的众多方法之一的摘录(基本上是将文件拖放到用户计算机中的特定位置)。在命令行中,或者最好在用户界面中,我希望允许用户输入他们的"源"目录,也就是说,在这种情况下,我的"user"目录是520,但对于其他用户来说,它可能会有所不同。我可以将其替换为允许Windows找出目录本身的星号,还是必须对目录进行硬编码?任何有软件开发经验的人请输入您的答案 - 谢谢。

我可以用星号替换它,允许 Windows 找出 目录本身,还是必须对目录进行硬编码?

如果您希望任何系统

为自己计算出用户目录(不仅仅是Windows,而是任何系统),您可以使用

String homeDirectory=System.getProperty("user.home");

然后,您可以在用户的主目录中创建自己的文件结构,请注意使用"foobar"作为"\"不是通用的,并且不适用于所有系统。然而,java再次用annother系统属性来拯救

String PATHSEPERATOR=System.getProperty("file.separator");

然后像这样使用:

homeDirectory=homeDirectory+"foo"+PATHSEPERATOR+"bar";

然后,您可以使用它来创建File对象

File homeDirectoryAsFile = new File(homeDirectory); 

最新更新