getResourceAsStream在eclipse中工作,但在浏览器中作为applet运行时却没有

时间:2011-04-02 23:45:30

标签: java eclipse jar applet getresource

我有一个applet需要从带有.java文件的eclipse包中的本地文本文件加载已保存的数据。要在此问题中使用,其名称为“saveData.txt”。

当我通过eclipse运行applet时,一切都运行良好。但是,一旦我把它(确保包含txt文件和.classpath文件)并将其放到网站上,就会无法加载。我在InputStreamReader上收到Unknown source错误。我已将我的代码包含在下面加载和保存。创建InputStreamReader Line时会出现错误。

我搞砸了什么想法?

private ArrayList loadLocations() throws IOException{
        ArrayList locations = new ArrayList();
        InputStream is = getClass().getResourceAsStream("\saveData.txt");

            ;; BELOW LINE is where exception is thrown
            InputStreamReader isr = new InputStreamReader(is);
    mInputStream = new BufferedReader(isr);
    String line = mInputStream.readLine();

    while (line != null && !(line.equals(""))) {
        locations.add(new Location(line));

        line = mInputStream.readLine();
    }
    if (mInputStream != null)
        mInputStream.close();   

    Collections.sort(locations);

    return locations;
}

private void saveLocations() throws IOException {
    try {
        mOutputStream = new PrintWriter(new File(getClass().getResource("\\saveData.txt").toURI()));
    }
    catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ;
    for (Location l : locations) {
        mOutputStream.print(l.getRawListing()+ "\n");
    }


    if (mOutputStream != null)
        mOutputStream.close();

}

完整错误详细信息(在浏览器中运行时)


Java Plug-in 1.6.0_24
Using JRE version 1.6.0_24-b07 Java HotSpot(TM) Client VM
User home directory = C:\Users\Ross
----------------------------------------------------
c:   clear console window
f:   finalize objects on finalization queue
g:   garbage collect
h:   display this help message
l:   dump classloader list
m:   print memory usage
o:   trigger logging
q:   hide console
r:   reload policy configuration
s:   dump system and deployment properties
t:   dump thread list
v:   dump thread stack
x:   clear classloader cache
0-5: set trace level to 
----------------------------------------------------


java.lang.reflect.InvocationTargetException
    at com.sun.deploy.util.DeployAWTUtil.invokeAndWait(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.runOnEDT(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
    at java.io.Reader.(Unknown Source)
    at java.io.InputStreamReader.(Unknown Source)
    at listings.listings.loadLocations(listings.java:624)
    at listings.listings.setup(listings.java:667)
    at listings.listings.(listings.java:59)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at sun.plugin2.applet.Plugin2Manager$12.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception: java.lang.reflect.InvocationTargetException

0 个答案:

没有答案