java - Path.equals 在 Windows 和 Linux 上的行为不同
问题描述
我使用以下代码来比较 Java 中的两个路径:
import java.nio.file.Paths;
public class PathTest {
public static void main(String args[]) {
String path1 = "path1\\file1.jpg";
String path2 = "path1/file1.jpg";
System.out.println(Paths.get(path1));
System.out.println(Paths.get(path2));
System.out.println(Paths.get(path1).equals(Paths.get(path2)));
}
}
我在我的 Windows 机器上得到以下输出:
path1\file1.jpg
path1\file1.jpg
true
在 Linux 上:
path1\file1.jpg
path1/file1.jpg
false
这里发生了什么?
解决方案
/
是 Unix 和类 Unix 系统(如 Linux)上的路径分隔符。现代 Windows 操作系统可以同时使用\
和/
作为路径分隔符。
推荐阅读
- magento2 - 核心模块中的 Magento 从 2.2.6 升级到 2.3.0 问题
- php - 如何将邮件发送到多个地址但按 20-30 分组以避免在 php 中发送垃圾邮件?
- python - 简单的__双下划线作为变量名是什么意思?只是__不跟随另一个字符
- drupal - Drupal 8 多个子域使用相同的数据库和不同的设计
- ckeditor - CKEditor 5 按属性查找模型元素
- php - 产品最佳动态菜单 3 标签 php mysqli
- c# - 将字符串转换为十六进制后字符串中的奇数十六进制长度
- java - 从文本文件中读取姓名/电话号码并将它们存储到java中的数组中
- mongodb - MongoDB:高效的 $nin 运算符
- android - 尝试从 DialogFragment 获取回调到片段,但应用程序崩溃