Java手册
separatorChar
public static final char separatorChar
- 与系统有关的默认名称分隔符。此字段被初始化为包含系统属性
file.separator
值的第一个字符。在 UNIX 系统上,此字段的值为'/'
;在 Microsoft Windows 系统上,它为'\\'
。
import java.io.File; public class FileDemo5 { public static void main(String[] args) { // 在Windows中,分隔目录用的\ // 在Linux中,分隔目录用的/ // 在Windows中,分隔路径用的; // 在Linux中,分隔路径用的: // pathSeparatorChar 代表 : // separatorChar 代表 / File file = new File("E:" + File.separatorChar + "aaa.txt"); // 获取父路径 System.out.println(file.getParent()); System.out.println(file.getName()); // 获取绝对路径 System.out.println(file.getAbsolutePath()); // 获取路径 System.out.println(file.getPath()); } }