📜  Java中的 Path endsWith() 方法及示例

📅  最后修改于: 2022-05-13 01:55:32.346000             🧑  作者: Mango

Java中的 Path endsWith() 方法及示例

Java.nio.file.Pathendswith()方法用于检查此路径对象是否以我们作为参数传递的给定路径或字符串。
有两种类型的 endsWith() 方法。

  1. Java.nio.file.PathendsWith(String other)方法用于检查此路径是否以 Path 结尾,通过将我们作为参数传递的给定路径字符串转换为该方法来构造。例如,此路径“dir1/file1”以“dir1/file1”和“file1”结尾。它不以“1”或“/file1”结尾。请注意,不考虑尾随分隔符,因此使用字符串“file1/”在路径“dir1/file1”上调用此方法会返回 true。

    句法:

    default boolean endsWith(String other)
    

    参数:此方法接受一个参数other是给定的路径字符串。

    返回值:如果此路径以给定路径结束,则此方法返回true;否则为假。

    异常:如果路径字符串无法转换为 Path,则此方法抛出InvalidPathException

    下面的程序说明了 endsWith(String other) 方法:
    方案一:

    // Java program to demonstrate
    // Path.endsWith(String other) method
      
    import java.nio.file.Path;
    import java.nio.file.Paths;
    public class GFG {
        public static void main(String[] args)
        {
      
            // create object of Path
            Path path = Paths.get("\\temp\\Spring");
      
            // create a string object
            String passedPath = "Spring";
      
            // call endsWith() to check path object
            // ends with passedPath or not
            boolean check = path.endsWith(passedPath);
      
            // print result
            System.out.println("Path ends with "
                               + passedPath + " :"
                               + check);
        }
    }
    
    输出:
  2. Java .nio.file.PathendsWith(Path other)方法用于检查此路径是否以给定路径作为方法的参数结束。如果此路径以给定路径结束,则该方法返回true;否则为假。
    如果传递的路径有 N 个元素,并且没有根组件并且此路径具有 N 个或更多元素,则如果每个路径的最后 N 个元素(从离根最远的元素开始)相等,则此路径以给定路径结束。
    如果传递的路径具有根组件,则如果此路径的根组件以给定路径的根组件结尾,则此路径以给定路径结尾,并且两条路径的对应元素相等。此路径的根组件是否以给定路径的根组件结尾是文件系统特定的。如果此路径没有根组件并且给定路径具有根组件,则此路径不会以给定路径结束。
    如果给定路径与该路径的不同 FileSystem 相关联,则返回 false。

    句法:

    boolean endsWith(Path other)
    

    参数:此方法接受一个参数,其他参数是给定的路径。

    返回值:如果此路径以给定路径结束,则此方法返回true;否则为假。

    下面的程序说明了 endsWith(Path other) 方法:
    方案一:

    // Java program to demonstrate
    // java.nio.file.Path.(Path other) method
      
    import java.nio.file.Path;
    import java.nio.file.Paths;
    public class GFG {
        public static void main(String[] args)
        {
      
            // create an object of Path
            Path path
                = Paths.get("D:\\eclipse"
                            + "\\plugins"
                            + "\\javax.xml.rpc_1.1.0.v201209140446"
                            + "\\lib");
      
            // create a path object which we will pass
            // to endsWith method to check functionality
            // of endsWith(Path other) method
            Path passedPath = Paths.get(
                "javax.xml.rpc_1.1.0.v201209140446"
                + "\\lib");
      
            // call endsWith() to check path object
            // ends with passedPath or not
            boolean check = path.endsWith(passedPath);
      
            // print result
            System.out.println("Path ends with "
                               + passedPath + " :"
                               + check);
        }
    }
    
    输出:

参考:

  • Java Java )
  • Java Java )