下面的 Java 代码在 Windows 机器上运行
filepath = "euro\football\france\winners.txt";
String[] values = StringUtils.split(filePath, "\\");
if (values != null && values.length >= 4) {
} else {
//error
}
但是在执行代码时在 linux 中遇到问题。 if 循环没有执行,else 循环正在执行。
对于 linux,我们是否需要将拆分作为“\”或“/”
String[] values = StringUtils.split(filePath, "\\");
任何建议都会有帮助
最佳答案
如果文件在运行 JVM 的机器上,那么您可以使用 File.separatorChar
获取本地机器的系统相关分隔符。
String[] values = StringUtils.split(filePath, File.separator);
JavaDoc说 (File.separatorChar
):
The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\'.
关于java - 字符串实用程序拆分 - Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51399127/