I\'d like to check if a Path (introduced in Java 7) ends with a certain extension. I tried the endsWith() method like so:
Path path = Paths.get
Simple solution:
if( path.toString().endsWith(".java") ) //Do something
You have to be carefull, when using the Path.endsWith method. As you stated, the method will return true only if it matches with a subelement of your Path object. For example:
Path p = Paths.get("C:/Users/Public/Mycode/HelloWorld.java");
System.out.println(p.endsWith(".java")); // false
System.out.println(p.endsWith("HelloWorld.java")); // true