为什么目录不存在?

时间:2014-05-19 21:05:35

标签: java

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

然后在构建之后几乎立即运行此代码:

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");

2 个答案:

答案 0 :(得分:1)

首先,您需要删除标题" /",然后所有n#34;%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 " "

答案 1 :(得分:-1)

我认为您应该在创建后关闭该文件。

f.close();

在输出行之前