Applet类中是getCodeBase()来获取的;那么Application中有功能和getCodeBase()相类似的方法吗

解决方案 »

  1.   

    直接new一个URL对象出来吧,构造方法:URL(String spec) 
              根据 String 表示形式创建 URL 对象。 
    URL(String protocol, String host, int port, String file) 
              根据指定 protocol、host、port 号和 file 创建 URL 对象。 
    URL(String protocol, String host, int port, String file, URLStreamHandler handler) 
              根据指定的 protocol、host、port 号、file 和 handler 创建 URL 对象。 
    URL(String protocol, String host, String file) 
              根据指定的 protocol 名称、host 名称和 file 名称创建 URL。 
    URL(URL context, String spec) 
              通过在指定的上下文中对给定的 spec 进行解析创建 URL。 
    URL(URL context, String spec, URLStreamHandler handler) 
              通过在指定的上下文中用指定的处理程序对给定的 spec 进行解析来创建 URL。
      

  2.   

    如果是读取自己磁盘上的目录下或者jar包中的文件,有一个可以用:
    URL url = this.getClass().getClassLoader().getResource("data.txt");
    得到在类加载器的目录下的文件如data.txt的URL.
    如你的程序没有把包,在目录D:/JavaCode/下,为Test.java
    那么类加载器得到的路径是D:/JavaCode/如果把包了,包名是com.xx.Text.java, 则你的源码在目录D:/JavaCode/com/xx下,为Test.java
    这时得到的类加载器的路径还是D:/JavaCode/
    如果这时你想得到D:/JavaCode/com/data.txt的URL,
    那么如下:
    URL url = this.getClass().getClassLoader().getResource("com/data.txt");