Wednesday, April 16, 2014

Be careful with Java Path.endsWith(String) usage

If you need to compare the java.io.file.Path object, be aware that Path.endsWith(String) will ONLY match another sub-element of Path object in your original path, not the path name string portion! If you want to match the string name portion, you would need to call the Path.toString() first. For example

// Match all jar files.
Files.walk(dir).forEach(path -> {
if (path.toString().endsWith(".jar"))
System.out.println(path);
});

With out the "toString()" you will spend many fruitless hours wonder why your program didn't work.

2 comments:

  1. Its actually a great and helpful piece of information. I am satisfied that you simply shared this helpful info with us.
    https://www.exltech.in/java-training.html

    ReplyDelete
  2. Great Post! it is very helpful for me and all for those who want to learn. Best Java Training Institute in Pune

    ReplyDelete