首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java : Windows文件上的File.toURI().toURL()

Java : Windows文件上的File.toURI().toURL()
EN

Stack Overflow用户
提问于 2009-07-15 13:03:28
回答 3查看 27.1K关注 0票数 16

我运行的系统是Windows XP,带有JRE 1.6。

我这样做:

public static void main(String[] args) {
    try {
        System.out.println(new File("C:\\test a.xml").toURI().toURL());
    } catch (Exception e) {
        e.printStackTrace();
    }       
}

我得到了这个:file:/C:/test%20a.xml

为什么给定的URL在C:前面没有两个斜杠?我期待的是file://C:...。这是正常行为吗?

编辑:

来自Java源代码: java.net.URLStreamHandler.toExternalForm(URL)

    result.append(":");
    if (u.getAuthority() != null && u.getAuthority().length() > 0) {
        result.append("//");
        result.append(u.getAuthority());
    }

文件URL的Authority部分似乎为null或为空,因此跳过了双斜杠。那么,什么是URL的权限部分,它是否真的在文件协议中缺失?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1131273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档