getResourceAsStream 在 Eclipse 中工作,但在浏览器中作为小程序运行时不工作

发布于 2024-10-29 18:18:39 字数 4206 浏览 1 评论 0原文

我有一个小程序,需要从 eclipse 包中的本地文本文件加载保存的数据以及 .java 文件。为了在这个问题中使用,它的名称是“saveData.txt”。

当我通过 eclipse 运行小程序时,一切都运行良好。但是,一旦我将其打包(确保包含 txt 文件和 .classpath 文件)并将其放到网站上,小程序就无法加载。我的 InputStreamReader 上收到未知来源错误。我在下面包含了用于加载和保存的代码。创建 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

I have an applet that needs to load saved data from a local text file that is in the eclipse package with the .java files. For use in this questions its name is "saveData.txt".

When I run the applet through eclipse, everything works perfectly. However, once I jar it up (making sure the txt file and .classpath files are included) and put it onto a website, the applet fails to load. I am getting an error of Unknown source on my InputStreamReader. I have included my code for loading and saving below. The error comes on the creation of the InputStreamReader Line.

Any Ideas where I screwed up?

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();

}

Full Error details (when run in browser)


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文