集册 Java实例教程 将路径转换为文件

将路径转换为文件

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

531
也可以使用toFile()方法将Path转换为File对象。

import java.io.File;

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

import java.nio.file.Paths;


public class Main {

  public static void main(String[] args) {

    Path path = Paths.get("/folder1/folder2/folder4", "test.txt");


    // convert path to File object

    File path_to_file = path.toFile();

    Path file_to_path = path_to_file.toPath();

    System.out.println("Path to file name: " + path_to_file.getName());

    System.out.println("File to path: " + file_to_path.toString());

  }

}