How to check the extension of a Java 7 Path

前端 未结 4 1181
温柔的废话
温柔的废话 2020-12-08 03:23

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         


        
4条回答
  •  盖世英雄少女心
    2020-12-08 04:05

    The Path class does not have a notion of "extension", probably because the file system itself does not have it. Which is why you need to check its String representation and see if it ends with the four five character string .java. Note that you need a different comparison than simple endsWith if you want to cover mixed case, such as ".JAVA" and ".Java":

    path.toString().toLowerCase().endsWith(".java");
    

提交回复
热议问题