集册 Java实例教程 创建路径对象并使用其方法来进一步了解路径。

创建路径对象并使用其方法来进一步了解路径。

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

478
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建一个Path对象并使用其方法进一步了解路径。

import java.nio.file.FileSystems;

import java.nio.file.Path;
/*来 自 nowjava - 时代Java*/

public class Main {


  public static void main(String[] args) {

    Path path = FileSystems.getDefault().getPath("/home/docs/status.txt");

    System.out.println();

    System.out.printf("toString: %s\n", path.toString());

    System.out.printf("getFileName: %s\n", path.getFileName());

    System.out.printf("getRoot: %s\n", path.getRoot());

    System.out.printf("getNameCount: %d\n", path.getNameCount());

    for (int index = 0; index < path.getNameCount(); index++) {

      System.out.printf("getName(%d): %s\n", index, path.getName(index));

    }

    System.out.printf("subpath(0,2): %s\n", path.subpath(0, 2));

    System.out.printf(
展开阅读全文