2.2.4 convert Path, 2.2.4 convert path

Source: Internet
Author: User

2.2.4 convert Path, 2.2.4 convert path

In NIO.2, you can easily merge paths, create paths in two paths, or compare paths;

Demo:

Import java. nio. file. path; import java. nio. file. paths; public class PathConvert {public static void main (String [] args) {// two [Max OS X] Paths Path path1 = Paths. get ("/Users/jinxing/Documents/pathtest/path1"); Path path2 = Paths. get ("/Users/jinxing/Documents/pathtest/path2"); // merge: Path completePath12 = path1.resolve ("ss/ss") is not added before the merged location "); // distance: Path path1ToPath2Dir = path1.relativize (path2) between two paths; // show System. out. println (completePath12); System. out. println (path1ToPath2Dir); // comparison: equals [Path. toString ()-comparison between two strings] System. out. println (completePath12.toString (). equals ("/Users/jinxing/Documents/pathtest/path1/ss"); // comparison: startsWith [Comparison Between paths | complete file name] (...) System. out. println (path1.startsWith ("/Users"); // comparison: endsWith [Comparison Between paths | complete file name | no separator before] (... end) System. out. println (path2.endsWith ("pathtest/path2"); // [omitted] More comparison tests: such as Path. equals (Path )}}

Ran As Java Application:

/Users/jinxing/Documents/pathtest/path1/ss/ss../path2truetruetrue

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.