Linux文件路徑分隔符爲 / ,windows的文件路徑分隔符爲 \ ,在開發項目過程中不確定用戶使用何種操作系統,就需要自動適配路徑。
目前已知Java提供兩種方法獲取文件路徑分割符:
File.separator
System.getProperty(“file.separator”)
public class FilePathUtil {
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
//public static final String FILE_SEPARATOR = File.separator;
public static String getRealFilePath(String path) {
return path.replace("/", FILE_SEPARATOR).replace("\\", FILE_SEPARATOR);
}
public static String getHttpURLPath(String path) {
return path.replace("\\", "/");
}
}