提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过标准化路径来消除冗余
//时 代 J a v a 公 众 号 - nowjava.com 提供 import java.nio.file.Path; import java.nio.file.Paths; public class Main { public static void main(String[] args) { Path path = Paths.get("/home/docs/../music/SpaceMachine A.mp3"); System.out.println("Absolute path: " + path.toAbsolutePath()); System.out.println("URI: " + path.toUri()); System.out.println("Normalized Path: " + path.normalize()); System.out.println("Normalized URI: " + path.normalize().toUri()); System.out.println(); path = Paths.get("/home/./music/ Robot Brain A.mp3"); System.out.println("Absolute path: " + path.toAbsolutePath()); System.out.println("URI: " + path.toUri())