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.File contains three methods for determining the file path, we will explore them in this tutorial.

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

  1. 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用作参数,则它将删除协议并返回文件名。
  2. 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系统上,通过将相对路径名与由该路径名命名的驱动器的当前目录(如果有)进行解析来使它成为绝对路径。 如果不是,则针对当前用户目录进行解析。

  3. 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 java.net.URI;
import java.net.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示例。

翻译自: https://www.journaldev.com/848/java-file-path-absolute-canonical

java绝对路径和相对路径

java绝对路径和相对路径_Java文件路径,绝对路径和规范路径相关推荐

  1. java 当前类相对路径_JAVA文件中获取该项目的相对路径方法

    1.基本概念的理解 绝对路径:绝对路径就是你的主页上的文件或目录在硬盘上真正的路径,(URL和物理路径)例如:C:\xyz\test.txt 代表了test.txt文件的绝对路径.http://www ...

  2. java多级目录文件是否存在_Java文件夹操作,判断多级路径是否存在,不存在就创建(包括windows和linux下的路径字符分析)...

    兼容windows和linux. 分析: 在windows下路径有以下表示方式: (标准)D:\test\1.txt (不标准,参考linux)D:/test/1.txt 然后在java中,尤其使用F ...

  3. java.nio.file 找不到_java - 断言该错误:无法访问路径(找不到java.nio.file.Path) - 堆栈内存溢出...

    我想使用Robolectric进行单元测试,但是我正在尝试使用robolectric进行简单测试,因此一开始我很困惑. 我遵循了手册,对示例进行了同样的操作,甚至其他帖子也无济于事. 每次收到错误消息 ...

  4. JAVA设置流中当前位置_java文件流的问题!急

    朋友你好,用使用 javadoc 标记可以做到.具体说明如下:: javadoc 标记是插入文档注释中的特殊标记,它们用于标识代码中的特殊引用.javadoc 标记由"@"及其后所 ...

  5. java在文本区输出方法_Java文件的几种读取、输出方式

    1.字节流----对文件读取(速度慢) /** * 字节流---文件的读取,输出(缺点:速度慢) * * @throws Exception */ @Test public void testIO1( ...

  6. phpcms文件夹plugin调用怎么写路径 - 代码篇

    phpcms文件夹statics/plugin/调用怎么写路径 - 代码篇 众所周知,用过phpcms框架的基本都熟悉APP_PATH的路径是http://localhost/,所以调用网站根目录ww ...

  7. java war目录_java war包 路径--解决war包中文件路径问题

    https://blog.csdn.net/u013409283/article/details/51480948 转自:http://free-chenwei.iteye.com/blog/1507 ...

  8. java读取绝对路径文件_Java 程序中使用相对和绝对路径读取文件

    在Java中用File对象来创建文件时,绝对路径一般是不会出现什么问题的,如果使用相对路径,则有可能出现意向不到的问题,以下主要是针对相对路径进行解释 1.Eclipse下的Java Project环 ...

  9. java文件放在哪里_Java文件路径

    几大常用的方法 Class.getResource("")    返回的是当前Class这个类所在包开始的位置 getClassLoader().getResource(" ...

最新文章

  1. 《数字孪生体技术白皮书(2019)》(简版)全文
  2. [转]volley-retrofit-okhttp之我们该如何选择网路框架
  3. C# 特性(Attribute)入门教程
  4. 第一天开始学习使用git中遇到的问题
  5. 《机器人学导论--Join J.Craig》第一章 绪论
  6. mysql怎么设置主键增长序列_mysql 如何设置自动增长序列 sequence(一)
  7. SUS安装配置简明图解攻略
  8. 【Nginx探究系列二】Nginx配置篇之客户Nginx白名单访问配置
  9. 自制导纳信号发生器 [原创cnblogs.com/helesheng]
  10. 多标签文本分类研究进展
  11. 美国旅游签证申请的行程单参考模板
  12. 自由软件运动与GNU项目
  13. collections.Counter 用法
  14. 华为社招嵌入式软件面试_华为社招面试感受 痛苦 悲催
  15. android 微信分享多张图片大小,Android 关于微信分享图片过大失败的解决方案
  16. 程序员技能树的分层分级方法
  17. 《草根自媒体达人运营实战》一一1.3 各种自媒体平台及优缺点
  18. InetAddress类中的getHostName()方法的坑
  19. 建立时间和保持时间概念
  20. 从程序员到CTO的Java技术路线图(我爱分享)

热门文章

  1. 在无法单步调试的情况下找Bug的技巧
  2. 第十八章 35用重载比较运算符实现字符串的比较
  3. [转载] python下 import Matplotlib.pyplot as plt 的使用
  4. [转载] log4j-over-slf4j与slf4j-log4j12共存stack overflow异常分析
  5. LeetCode-C#实现-链表(#19/21/141/206/707/876)
  6. go语言中文网中的资源
  7. CentOS7更改时区两步解决
  8. UWP 自然灾害App在刷新数据后卡死的解决方案
  9. Android ClassLoader笔记(二)
  10. hive-0.11.0安装方法具体解释