1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java绝对路径和相对路径_Java文件路径 绝对路径和规范路径

java绝对路径和相对路径_Java文件路径 绝对路径和规范路径

时间:2022-12-06 16:30:32

相关推荐

java绝对路径和相对路径_Java文件路径 绝对路径和规范路径

java绝对路径和相对路径

Today we will look into the Java file path. Java File path can be abstract, absolute or canonical.

今天,我们将研究Java文件路径。 Java File路径可以是抽象的,绝对的或规范的。

Java文件路径 (Java File Path)

java.io.Filecontains three methods for determining the file path, we will explore them in this tutorial.

java.io.File包含三种确定文件路径的方法,我们将在本教程中对其进行探讨。

getPath(): This file path method returns the abstract pathname as String. If String pathname is used to create File object, it simply returns the pathname argument. If URI is used as argument then it removes the protocol and returns the file name.getPath():此文件路径方法将抽象路径名作为String返回。 如果使用String路径名创建File对象,则仅返回pathname参数。 如果将URI用作参数,则它将删除协议并返回文件名。getAbsolutePath(): This file path method returns the absolute path of the file. If File is created with absolute pathname, it simply returns the pathname.

If the file object is created using a relative path, the absolute pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory.

On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

getAbsolutePath():此文件路径方法返回文件的绝对路径。 如果使用绝对路径名创建File,则它仅返回路径名。

如果使用相对路径创建文件对象,则以系统相关的方式解析绝对路径名。 在UNIX系统上,通过相对于当前用户目录解析相对路径名来使它成为绝对路径。

在Microsoft Windows系统上,通过将相对路径名与由该路径名命名的驱动器的当前目录(如果有)进行解析来使它成为绝对路径。 如果不是,则针对当前用户目录进行解析。

getCanonicalPath(): This path method returns the canonical pathname that is both absolute and unique. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath method, and then maps it to its unique form in a system-dependent way.

This typically involves removing redundant names such as “.” and “..” from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

getCanonicalPath ():此路径方法返回绝对且唯一的规范路径名。 如有必要,此方法首先将此路径名转换为绝对形式,就像通过调用getAbsolutePath方法一样,然后以与系统有关的方式将其映射为其唯一形式。

这通常涉及删除多余的名称,例如“。”。 和(..)从路径名开始,解析符号链接(在UNIX平台上),并将驱动器号转换为标准大小写(在Microsoft Windows平台上)。

Java文件路径示例 (Java File Path Example)

Let’s see different cases of the file path in java with a simple program.

让我们用一个简单的程序查看java中文件路径的不同情况。

package com.journaldev.files;import java.io.File;import java.io.IOException;import .URI;import .URISyntaxException;public class JavaFilePath {public static void main(String[] args) throws IOException, URISyntaxException {File file = new File("/Users/pankaj/test.txt");printPaths(file);// relative pathfile = new File("test.xsd");printPaths(file);// complex relative pathsfile = new File("/Users/pankaj/../pankaj/test.txt");printPaths(file);// URI pathsfile = new File(new URI("file:///Users/pankaj/test.txt"));printPaths(file);}private static void printPaths(File file) throws IOException {System.out.println("Absolute Path: " + file.getAbsolutePath());System.out.println("Canonical Path: " + file.getCanonicalPath());System.out.println("Path: " + file.getPath());}}

Below image shows the output produced by the above java file path program.

下图显示了上述java文件路径程序产生的输出。

The output is self-explanatory. Based on the output, using the canonical path is best suitable to avoid any issues because of relative paths.

输出是不言自明的。 基于输出,使用规范路径最适合避免相对路径引起的任何问题。

Also, note that the java file path methods don’t check if the file exists or not. They just work on the pathname of the file used while creating the File object. That’s all for different types of the file path in java.

另外,请注意,java文件路径方法不会检查文件是否存在。 它们仅在创建File对象时使用的文件路径名上工作。 这就是java中不同类型的文件路径的全部。

GitHub Repository.GitHub存储库中签出更多的Java IO示例。

翻译自: /848/java-file-path-absolute-canonical

java绝对路径和相对路径

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。