当前位置: 首页 > news >正文

System.getProperty(“user.dir“)获取用户工作目录及绝对路径和相对路径的说明

所有在java.io中的类都将相对路径名解释为以用户工作目录开始,可以通过调用System.getProperty(“user.dir”)来获取这个信息

项目结构

在这里插入图片描述

代码

package com.burns.test;

import java.io.File;
import java.io.FileInputStream;

public class FileTest {
    public static void main(String[] args) throws Exception{
        String property = System.getProperty("user.dir");
        System.out.println(property);

        FileInputStream fis = null;
        fis = new FileInputStream("C:\\Users\\ceshi\\Desktop\\tencent970923110695913725.txt");
        File file = new File("pom.xml");
        System.out.println(file.getAbsolutePath());

        File file4 = new File("src/main/java/com/burns/test/FileTest.java");
        System.out.println(file4.getAbsolutePath());

        File file3 = new File("\\pom.xml");
        System.out.println(file3.getAbsolutePath());

        File file1 = new File("..\\pom.xml");
        System.out.println(file1.getAbsolutePath());

        File file2 = new File(".\\pom.xml");
        System.out.println(file2.getAbsolutePath());

        fis = new FileInputStream("pom.xml");
//        读取fis
        byte[] bytes = new byte[1024];
        while (fis.read(bytes) != -1){
            System.out.println(new String(bytes));
        }
    }
}



输出

D:\workspace\idea_proj\jdk_demo_20250311\jdk_demo_8
D:\workspace\idea_proj\jdk_demo_20250311\jdk_demo_8\pom.xml
D:\workspace\idea_proj\jdk_demo_20250311\jdk_demo_8\src\main\java\com\burns\test\FileTest.java
D:\pom.xml
D:\workspace\idea_proj\jdk_demo_20250311\jdk_demo_8\..\pom.xml
D:\workspace\idea_proj\jdk_demo_20250311\jdk_demo_8\.\pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.burns</groupId>
        <artifactId>jdk_demo_20250311</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <groupId>com.burns.jdk8</groupId>
    <artifactId>jdk_demo_8</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>                                                                                                                                                                                                                                                     

整理总结

在 Java 中,使用 File 类创建文件对象时,路径可以是全路径(绝对路径)或相对路径。理解这两种路径的区别以及相对路径前面加 \\ 的含义非常重要。以下是详细的解释:

全路径(绝对路径)

  • 定义:全路径是从文件系统的根目录开始的完整路径。
  • 示例
    • Windows: C:\\Users\\Username\\Documents\\file.txt
    • Unix/Linux/Mac: /home/username/documents/file.txt

相对路径

  • 定义:相对路径是相对于当前工作目录的路径。
  • 当前工作目录:通常是运行 Java 程序的目录。
  • 示例
    • 相对于当前工作目录的文件:documents/file.txt
    • 相对于当前工作目录的子目录:subdir/file.txt

相对路径前面加 \\

在 Windows 系统中,路径分隔符通常是反斜杠 \。然而,在 Java 字符串中,反斜杠 \ 是转义字符。因此,如果你在字符串中直接使用反斜杠,需要进行转义。

  • 转义反斜杠:在字符串中使用双反斜杠 \\ 来表示一个反斜杠。
  • 示例
    • 错误的路径表示:"C:\Users\Username\Documents\file.txt"(会导致编译错误)
    • 正确的路径表示:"C:\\Users\\Username\\Documents\\file.txt"

示例代码

使用全路径
import java.io.File;

public class FileExample {
    public static void main(String[] args) {
        // Windows 全路径
        File file = new File("C:\\Users\\Username\\Documents\\file.txt");
        System.out.println("File exists: " + file.exists());

        // Unix/Linux/Mac 全路径
        // File file = new File("/home/username/documents/file.txt");
        // System.out.println("File exists: " + file.exists());
    }
}
使用相对路径
import java.io.File;

public class FileExample {
    public static void main(String[] args) {
        // 相对路径
        File file = new File("documents/file.txt");
        System.out.println("File exists: " + file.exists());
    }
}

当前工作目录

  • 获取当前工作目录
    • 使用 System.getProperty("user.dir") 可以获取当前工作目录。
  • 示例
import java.io.File;

public class CurrentDirectoryExample {
    public static void main(String[] args) {
        String currentDir = System.getProperty("user.dir");
        System.out.println("Current Directory: " + currentDir);

        // 使用相对路径
        File file = new File("documents/file.txt");
        System.out.println("File exists: " + file.exists());
    }
}

总结

  • 全路径:从文件系统的根目录开始的完整路径。
  • 相对路径:相对于当前工作目录的路径。
  • 转义反斜杠:在 Windows 系统中,路径分隔符 \ 需要转义为 \\
  • 当前工作目录:可以通过 System.getProperty("user.dir") 获取。

通过理解这些概念,你可以更准确地指定文件路径,避免路径相关的错误。以下是表格总结:

路径类型定义示例注意事项
全路径从文件系统的根目录开始的完整路径C:\\Users\\Username\\Documents\\file.txt
/home/username/documents/file.txt
使用绝对路径时,确保路径正确且文件存在。
相对路径相对于当前工作目录的路径documents/file.txt
subdir/file.txt
当前工作目录可以通过 System.getProperty("user.dir") 获取。
转义反斜杠在 Windows 系统中,路径分隔符 \ 需要转义为 \\"C:\\Users\\Username\\Documents\\file.txt"在字符串中使用双反斜杠 \\ 来表示一个反斜杠。

希望这些解释和示例代码能帮助你更好地理解和使用 Java 中的文件路径。

http://www.dtcms.com/a/79293.html

相关文章:

  • Linux驱动学习笔记(一)
  • 爬虫 crawler 入门爬取不设防网页 并实现无限增生
  • DeepSeek 助力 Vue3 开发:打造丝滑的表格(Table)之添加导出数据功能示例4,TableView15_04导出当前页数据示例
  • C++ 入门第27天:异常处理详细讲解
  • 麒麟V10 arm cpu aarch64 下编译 RocketMQ-Client-CPP 2.2.0
  • 【OCR】总结github上开源 OCR 工具:让文字识别更简单
  • YOLO obb全流程
  • mapbox-gl源码中解析style加载地图过程详解
  • win本地部署Dify,并接入deepseek-r1
  • 【Java集合夜话】第1篇:拨开迷雾,探寻集合框架的精妙设计
  • 蓝桥杯_拔河_java
  • Flutter:页面滚动,导航栏背景颜色过渡动画
  • 前后端项目
  • 创新实训项目初始化——gitee的使用
  • 一文了解ThreadLocal
  • 蓝桥杯 因数计数
  • 卷积神经网络 - 汇聚层
  • centos8-安装R+ggplot2
  • Dify:开源大模型应用开发平台全解析
  • 1-1 MATLAB深度极限学习机
  • ‌React Hooks主要解决什么
  • docker pull 提示timeout
  • PHP:从入门到进阶的编程之旅
  • MyBatis源码分析のSql执行流程
  • Rust学习之实现命令行小工具minigrep(一)
  • 从零开始写C++3D游戏引擎(开发环境VS2022+OpenGL)之十一点二五 光照贴图(lighting maps)的实现 细嚼慢咽逐条读代码系列
  • nacos安装,服务注册,服务发现,远程调用3个方法
  • 理一理Mysql日期
  • [原创](Modern C++)现代C++的关键性概念: std::mem_fn是std::bind的轻量级版本, 它们的区别是什么?
  • 蓝桥杯嵌入式赛道复习笔记4(TIM输出PWM,TIM输入捕获)