集册 Java实例教程 确定两条路径是否相等

确定两条路径是否相等

欢马劈雪     最近更新时间:2020-01-02 10:19:05

509
确定两条路径是否等效

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Path;//时代Java - nowjava.com 提供

import java.nio.file.Paths;


public class Main {


  public static void main(String[] args) {

    Path path1 = null;

    Path path2 = null;

    Path path3 = null;


    path1 = Paths.get("/home/docs/users.txt");/**from nowjava - 时  代  Java**/

    path2 = Paths.get("/home/docs/users.txt");

    path3 = Paths.get("/home/music/Future Setting A.mp3");


    testEquals(path1, path2);

    testEquals(path1, path3);


    testCompareTo(path1, path2);

    testCompareTo(path1, path3);


    testSameFile(path1, path2);

    testSameFile(path1, path3);

  }


  private static void testEquals(Path path1, Path path2) {

    if (path1.equals(path2)) {

      System.out.printf("%s and %s are equal\n", path1, path2);

    } else {

      System.out.printf("%s and %s are NOT equal\n", path1, path2);

    }

  }


  private static void testCompareTo(Path path1, Path path2) {

    if (path1.compareTo(path2) == 0) {

      System.out.printf("%s and %s are identical\n", path1, path2);

    }


    else {

      System.out.printf("%s and %s are NOT identical\n", path1, path2);

    }

  }


  private static void testSameFile(Path path1, Path path2) {

    try {

      if (Files.i
展开阅读全文