-1

If I already have an existing file and I want to know its path using only its name, how can I do this?

I have the following code, but it return the name of the file even when it does not exists:

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.{java,class}");

Path filename = Paths.get("Go,mvDep.java");
if (matcher.matches(filename)) {
    System.out.println(filename);
}

Thank you for your help!

Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
user5139637
  • 775
  • 3
  • 10
  • 29
  • What do you mean _even when it does not exists_? Exist where? Do you mean on the file system? What part of your code do you think should check for the existence of the file? Why do you think so? – Sotirios Delimanolis Nov 29 '15 at 02:16
  • Yes, I mean in the file system. I see what you are saying. But is there a method that given a file name, it will return the path of it by searching it in the whole system? – user5139637 Nov 29 '15 at 02:18
  • Check this out: http://stackoverflow.com/a/15624430/3794552 – Bimde Nov 29 '15 at 02:25

1 Answers1

0

I think the core of your confusion is that a Path does not necessarily represent a file on your computer. It is simply a Java object that represents a conceptual object in a file system. In the same way that you could construct a new Person("John", "Smith") without actually knowing anyone named 'John Smith', you can construct a Path regardless of whether or not a file exists at the given location.

Once you have a Path there are a number of things you can do with it, including check if it exists via Files.exists(), or create it with Files.createFile(). Generally speaking, the Files class lets you inspect and work with the actual file system objects a Path represents.

The intent of a PathMatcher is similarly disconnected from the actual file system; it exists to to determine if a given Path fits the PathMatcher's pattern - it's basically a Path-specific regular expression engine.

So what your code is actually doing is:

  1. Creating a glob that will match any path which ends in .java or .class (regardless of whether such a path exists anywhere).
  2. Constructing a relative Path to a file called Go,mvDep.java. Implicitly this path is relative to the current working directory, but you could pass it to Path.resolve() to create a new Path referring to a file in a different location.
  3. Checking if the path Go,mvDep.java matches your glob, which it does since it ends in .java, so it prints the path.

It sounds like what you actually want is to find an existing file with the name Go,mvDep.java. If so, you want to use Files.find() to search a directory and return a stream of the files that match a BiPredicate<Path, BasicFileAttributes> matcher you define. Your matcher might look something like this:

new BiPredicate<Path, BasicFileAttributes>() {
  public boolean test(Path path, BasicFileAttributes attributes) {
    return matcher.matches(path);
  }
}

Or in Lambda syntax simply:

(p, a) -> matcher.matches(p)
dimo414
  • 47,227
  • 18
  • 148
  • 244