Finding the right Path

三世轮回 提交于 2019-12-13 04:01:29

问题


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.

My project structure looks like this:

  • build
  • nbprojects
  • docs
  • src <-- this is where the generated files belong

    • java
      • generator
        • generator.java <-- this is where the files are being generated
  • [some other folders]

When I try:

System.getProperty("user.dir");

I get (I'm on Linux):

/home/user1/.netbeans/9.0/config/GF_5.0/domain1/config

My guess was going 3 levels up:

../../../docs/file1.pdf

But it says

java.io.FileNotFoundException: ../../../docs/file1.pdf was not found

Question: How can I access docs from src/java/generator/generator.java?


回答1:


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

It will print something like,

\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.



来源:https://stackoverflow.com/questions/52919689/finding-the-right-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!