JAVA自动适配Linux与Windows文件路径分隔符

Linux文件路径分隔符为 / ,windows的文件路径分隔符为 \ ,在开发项目过程中不确定用户使用何种操作系统,就需要自动适配路径

public class FilePathUtil {  
    public static final String FILE_SEPARATOR = System.getProperty("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("\\", "/");  
    }  
} 

 

posted @ 2022-03-20 11:34  Sherlock先生  阅读(1180)  评论(0)    收藏  举报