Java 对windows/linux不同路径的处理办法

简介: 本文目录1. 概述2. 跨平台的路径写法

1. 概述

之前写过一篇博客,表述了windows与linux下路径区别,见:

[Python学习笔记(17)-windows和linux下的路径与文件](http://blog.csdn.net/woshisangsang/article/details/73739787)


具体点就是:


"C:\\a.txt"//windows写法

"/mnt/a.txt"//linux写法


2. 跨平台的路径写法

Java是跨平台的语言,路径写法也提供了跨平台的写法,使用File.separator即可。


如下示例,使用separator的明显更优雅也更实用。


import java.io.File;

//路径分隔符示例

public class SeparatorDemo {

public static void main(String[] args) {

 String path1="C:"+File.separator+"Program Files"+File.separator+"a.txt";

 System.out.println(path1);//输出C:\Program Files\a.txt

 

 String path2="C:\\Program Files\\a.txt";//第一个\表示转义

 System.out.println(path2);//输出C:\Program Files\a.txt

}

}

相关文章
|
6天前
|
安全 Unix Linux
Veritas NetBackup 11 for Linux & Windows - 领先的企业备份和恢复解决方案
Veritas NetBackup 11 for Linux & Windows - 领先的企业备份和恢复解决方案
20 0
Veritas NetBackup 11 for Linux & Windows - 领先的企业备份和恢复解决方案
|
5天前
|
Unix Linux 编译器
windows下和linux下cmake的规则有区别吗
通过合理使用CMake的条件逻辑和平台特定的配置选项,开发者可以编写更加灵活和健壮的CMake脚本,确保项目在Windows和Linux上的一致性和可移植性。
103 76
|
1天前
|
NoSQL IDE MongoDB
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
42 2
Studio 3T 2025.5 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
10天前
|
自然语言处理 数据库 iOS开发
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
63 12
DBeaver Ultimate Edtion 25.0 Multilingual (macOS, Linux, Windows) - 通用数据库工具
|
1天前
|
XML 安全 Ubuntu
Nexpose 8.0.0 for Linux & Windows - 漏洞扫描
Nexpose 8.0.0 for Linux & Windows - 漏洞扫描
30 10
|
1月前
|
人工智能 Linux iOS开发
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
71 12
Burp Suite Professional 2025.2 (macOS, Linux, Windows) - Web 应用安全、测试和扫描
|
21天前
|
Linux 虚拟化 Docker
Linux服务器部署docker windows
在当今软件开发中,Docker成为流行的虚拟化技术,支持在Linux服务器上运行Windows容器。流程包括:1) 安装Docker;2) 配置支持Windows容器;3) 获取Windows镜像;4) 运行Windows容器;5) 验证容器状态。通过这些步骤,你可以在Linux环境中顺利部署和管理Windows应用,提高开发和运维效率。
79 1
|
22天前
|
NoSQL IDE MongoDB
Studio 3T 2025.4 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
Studio 3T 2025.4 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
33 0
Studio 3T 2025.4 (macOS, Linux, Windows) - MongoDB 的终极 GUI、IDE 和 客户端
|
22天前
|
安全 Linux iOS开发
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
39 0
Gitea Enterprise 23.4.0 (Linux, macOS, Windows) - 本地部署的企业级 Gti 服务
|
26天前
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析
26 0
Splunk Enterprise 9.4.1 (macOS, Linux, Windows) 发布 - 机器数据管理和分析