0

I have an XML file in a folder within my Java project, and I'd like to get its absolute path, so I can load it as a File in order to parse it(DOM). Instead of using an absolute/relative path, I want to specify only the file name, and get the absolute path after that. I tried to do this in a few different ways, but there is always a folder name missing from the path I get.

I get:

C:\Users\user\workspace\projectName\Input.xml<br>

instead of:

C:\Users\user\workspace\projectName\\**Folder1**\\Input.xml

-

File input = new File(project.getFile("Input.xml").getLocation().toString());`
File input = new File(project.getFile("Input.xml").getRawLocation().makeAbsolute().toString());
File input = new File(project.getFile("Input.xml").getLocationURI().getRawPath().toString());
File input = new File(project.getFile("Input.xml").getFullPath().toFile().getAbsolutePath());

How can I get the correct path, that includes that Folder1?

freedev
  • 25,946
  • 8
  • 108
  • 125
ERX95
  • 87
  • 1
  • 10
  • 1
    How about using [`getResourceAsStream()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getResourceAsStream-java.lang.String-)? – Axel Apr 06 '17 at 08:02
  • `getResourceAsStream()` only works if the file is a resource included in the classpath, which _might_ not be the case – rrobby86 Apr 06 '17 at 08:05
  • If you have a `File` object `dir` representing a folder, you can reference a file `foo` inside it with `new File(dir, "foo")`, but how to apply this also depends from what the `project` object is. – rrobby86 Apr 06 '17 at 08:07
  • There are three variants of solution, depending on the situation: https://stackoverflow.com/a/56327069/715269 – Gangnus May 27 '19 at 13:37

1 Answers1

0

Reading your question (your project are in workspace directory) I suppose you're talking of a project in Eclipse.

Well the default directory where your app run into Eclipse is right the base dir of your project.

So if you run something like this in your main:

Files.newDirectoryStream(Paths.get("."))
     .forEach(path -> {
       System.out.println(path);
       System.out.println(path.toFile().getAbsolutePath());
     });

You should see all the files and directory that are in your project.

So if what you want is just the absolute path to your project run:

System.out.println(Paths.get(".").toFile().getAbsolutePath());

If you want open the resource Input.xml specifying only the name, I suggest to move all the files you need in a directory and run a method like this:

  public static File getFileByName(String name, String path) throws IOException {
    ArrayList<File> files = new ArrayList<>();
    Files.newDirectoryStream(Paths.get(path))
         .forEach(p -> {
           if (p.getFileName()
                   .equals(name))
             files.add(p.toFile());
         });
    return files.size() > 0 ? files.get(0) : null;
  }
freedev
  • 25,946
  • 8
  • 108
  • 125
  • It's gonna be some sort of a plugin, and I don't want to depend on relative paths. My method will receive only the file name it has to look for. There is no way to do that? – ERX95 Apr 06 '17 at 08:19
  • Why do not search into a directory for your file? I've just updated my answer. – freedev Apr 06 '17 at 08:24