集册 Java实例教程 将相对路径转换为绝对路径

将相对路径转换为绝对路径

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

694
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将相对路径转换为绝对路径

import java.net.URI;

import java.net.URISyntaxException;

import java.nio.file.FileSystems;

import java.nio.file.InvalidPathException;
/** 
 来自 n o w j a v a . c o m - 时  代  Java**/

import java.nio.file.Path;

import java.nio.file.Paths;


public class Main {

  public static void main(String[] args) {

    String separator = FileSystems.getDefault().getSeparator();

    System.out.println("The separator is " + separator);

    try {

      Path path = Paths.get(new URI("file:///C:/home/docs/users.txt"));

      System.out.println("subpath: " + path.subpath(0, 3));

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

      System.out.println("Absolute path: " + path.toAbsolutePath());

      System.out.println("URI: " + path.toUri());

    } catch (URISyntaxException ex) {

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