找到正确的道路 [英] Finding the right Path
问题描述
我想使用 Apache PDFBox 生成PDF,并将其放在docs/file1.pdf
下.文件夹docs
已经创建,但是我不知道如何从Java访问该文件夹.
I want to generate a PDF with Apache PDFBox and put it under docs/file1.pdf
. The folder docs
is already created, but I don't know how to access it from Java.
我的项目结构如下:
-
build
-
nbprojects
-
docs
-
src
<-这是生成的文件所属的地方
build
nbprojects
docs
src
<-- this is where the generated files belong
-
java
-
generator
-
generator.java
<-这是生成文件的地方
java
generator
generator.java
<-- this is where the files are being generated
[其他一些文件夹]
[some other folders]
当我尝试时:
System.getProperty("user.dir");
我得到了(我在Linux上):
I get (I'm on Linux):
/home/user1/.netbeans/9.0/config/GF_5.0/domain1/config
我的猜测上升了3个级别:
My guess was going 3 levels up:
../../../docs/file1.pdf
但它说
java.io.FileNotFoundException:找不到../../../docs/file1.pdf
java.io.FileNotFoundException: ../../../docs/file1.pdf was not found
问题:如何从
src/java/generator/generator.java
访问docs
?推荐答案
一种方法是使用绝对路径.或者,您可以使用此打印语句根据您的Java程序来了解当前目录,
One way is to use absolute path. Or you can use this print statement to know your current directory as per your java program,
System.out.println("Current Dir: " + new File(".").getAbsolutePath());
它将打印类似的内容,
\pkr\work7\ws\test\.
一旦您知道该路径,就可以通过放置..来正确构建路径,并能够正确读取文件.
Once you know this path, you can build your path correctly by putting .. and be able to correctly read your file.
这篇关于找到正确的道路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
-