Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Path equals() method in Java with Examples
Next article icon

Path endsWith() method in Java with Examples

Last Updated : 13 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

endswith() method of java.nio.file.Path used to check if this path object ends with the given path or string which we passed as parameter. There are two types of endsWith() methods.

  1. endsWith(String other) method of java.nio.file.Path used to check if this path ends with a Path, constructed by converting the given path string which we passed as a parameter to this method. For example, this path "dir1/file1" ends with "dir1/file1" and "file1". It does not end with "1" or "/file1". Note that trailing separators are not taken into account, and so invoking this method on the Path"dir1/file1" with the String "file1/" returns true. Syntax:
default boolean endsWith(String other)
  1. Parameters: This method accepts a single parameter other which is the given path string. Return value: This method returns true if this path ends with the given path; otherwise false. Exception: This method throws InvalidPathException If the path string cannot be converted to a Path. Below programs illustrate endsWith(String other) method: Program 1: 
Java
// 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);     } } 
Output:
  1. endsWith(Path other) method of java.nio.file.Path used to check if this path ends with the given path as parameter to method or not.This method return true if this path ends with the given path; otherwise false. If the passed path has N elements, and no root component and this path have N or more elements, then this path ends with the given path if the last N elements of each path, starting at the element farthest from the root, are equal. If the passed path has a root component then this path ends with the given path if the root component of this path ends with the root component of the given path, and the corresponding elements of both paths are equal. Whether or not the root component of this path ends with the root component of the given path is filesystem-specific. If this path does not have a root component and the given path has a root component then this path does not end with the given path. If the given path is associated with a different FileSystem to this path then false is returned. Syntax:
boolean endsWith(Path other)
  1. Parameters: This method accepts a single parameter other which is the given path. Return value: This method return true if this path ends with the given path; otherwise false. Below programs illustrate endsWith(Path other) method: Program 1: 
Java
// Java program to demonstrate // java.nio.file.Path.endsWith(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);     } } 
Output:

References:

  • https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#endsWith(java.lang.String)
  • https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#endsWith(java.nio.file.Path)

Next Article
Path equals() method in Java with Examples

A

AmanSingh2210
Improve
Article Tags :
  • Java
  • Java-Functions
  • Java-Path
  • java.nio.file package
Practice Tags :
  • Java

Similar Reads

  • Path equals() method in Java with Examples
    The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di
    2 min read
  • Path compareTo() method in Java with Examples
    The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di
    3 min read
  • Pattern asPredicate() Method in Java with Examples
    asPredicate() method of a Pattern class used to creates a predicate object which can be used to match a string.Predicate is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. Syntax: public Predicate asPredicate() Parameters: This m
    2 min read
  • Path toUri() method in Java with Examples
    The toUri() method of java.nio.file.Path interface used to return a URI to represent this path. This method converts this path into an absolute URI with a scheme equal to the URI scheme that identifies the provider. The exact form of the scheme-specific part is highly provider dependent. In the scen
    2 min read
  • Static Method in Java With Examples
    In Java, the static keyword is used to create methods that belongs to the class rather than any specific instance of the class. Any method that uses the static keyword is referred to as a static method. Features of Static Method: A static method in Java is associated with the class, not with any obj
    3 min read
  • Path subpath() method in Java with Examples
    The subpath(int beginIndex, int endIndex) method of java.nio.file.Path used to return a relative Path that is a subsequence of the name elements of this path. We will pass begin and end indexes to construct a subpath. The beginIndex and endIndex parameters specify the subsequence of name elements. T
    2 min read
  • Path resolve() method in Java with Examples
    resolve() method of java.nio.file.Path used to resolve the given path against this path. There are two types of resolve() methods. resolve(String other) method of java.nio.file.Path used to converts a given path string to a Path and resolves it against this Path in the exact same manner as specified
    3 min read
  • Path getRoot() method in Java with Examples
    The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a di
    2 min read
  • Path toFile() method in Java with Examples
    The toFile() method of java.nio.file.Path interface used to return a java.io.File object representing this path object. if this Path is associated with the default provider, then this method returns a java.io.File object constructed with the String representation of this path. If this path was creat
    2 min read
  • CompoundName endsWith() method in Java with Examples
    The endsWith() method of a javax.naming.CompoundName class is used to check whether compound name which is passed as a parameter is a suffix of this compound name or not. A compound name 'X' is a suffix of this compound name if this compound name object ends with 'X'. If X is null or not a compound
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences