如C:\Documents and Settings路径包含空格,直接传入到URI中会出错。
正确的做法:通过API将普通文件对象转换为URI路径。
而且,会自动给文件路径前面加上协议:file:/
URI文件路径与File之间的转换,将日志放到用户目录下:
public static void main(String[] args) { String home = System.getProperty("user.home"); File file = new File(home, "logs"); URI uri = file.toURI(); String filePath = uri.toString(); System.out.println(filePath); //---file:/C:/Documents%20and%20Settings/user001/logs }