Java 为什么';t目录存在吗?

Java 为什么';t目录存在吗?,java,Java,我试图获取Netbeans创建的临时目录的位置。当我单击“运行”运行我的项目时,它会在dist目录中创建一个文件,如run1017994493 然后,它几乎在构建之后立即运行以下代码: String fileloc = SQLite.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String location = fileloc.substring(0, fileloc.lastIndexOf("/"))

我试图获取Netbeans创建的临时目录的位置。当我单击“运行”运行我的项目时,它会在
dist
目录中创建一个文件,如
run1017994493

然后,它几乎在构建之后立即运行以下代码:

String fileloc = SQLite.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String location = fileloc.substring(0, fileloc.lastIndexOf("/"));
File f = new File(location);
System.out.println(location);
System.out.println(f.exists());
输出:

/C:/Users/rnaddy/Documents/NetBeansProjects/Phantom%20Browser/dist/run1017994493
false
为什么说目录不存在

我认为这导致以下两条线断裂:

Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:" + location + "/phantom.db");

首先,您需要删除标题“/”,然后所有出现的“%20”或类似内容都应替换为相应的字符:

String loc = ...
if (isWindows()) {
    //remove heading slash
    loc = loc.replaceAll("^/", "");
}
loc = URLDecoder.decode (loc, "UTF-8"); // this will replace all characters like "%20" with something like " "

尝试用spaceGenius替换%20!这就是原因!