JApplet 写入文本文件,destroy() / stop()

发布于 2024-10-28 23:37:25 字数 1191 浏览 4 评论 0原文

我正在尝试编写一个 JApplet,它使用文本文件中的信息来加载和保存数据。我已成功让小程序加载信息,但保存似乎出现问题。我已在下面包含要保存的代码。我使用的文件名与我用来写入的文件名相同。当我运行时,该文件必须包含在 JAR 中,因为小程序会正确初始化。有什么原因导致书写不能正常工作吗?我已经求助于从 stop() 和 destroy() 方法调用此方法。

请注意,从 eclipse 运行时,加载和保存都可以完美工作,但是在 JAR 中时,只有加载可以工作,但不会保存任何内容,因此我无法更改加载数据。

理想情况下,我希望每当页面关闭或刷新时都调用此 saveLocations() 方法。

注意:mOUTputStream确实是一个PrintWriter(它曾经是一个OutputStream,我想我应该更改名称)

提前非常感谢您的帮助。


private void saveLocations() throws IOException {
        //JOptionPane.showMessageDialog(null, "Alert", "Saving", JOptionPane.INFORMATION_MESSAGE);
        // System.out.println("saving!");
        try {
            mOutputStream = new PrintWriter(new File(getClass().getResource("/listings/saveData.txt").toURI()));
        }
        catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //System.out.println(locations.size());
        for (Location l : locations) {
            System.out.println("r: " + l.getRawListing());
            mOutputStream.print(l.getRawListing()+ "\n");
        }


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

    }

I am attempting to write a JApplet that uses information in a text file to load and save data from. I have successfully got the applet to load the information, but the saving appears to be having issues. I have included the code to save below. the file name I am using is the same as I use to write to. The file must be included in the JAR when I run because the applet initializes properly. Is there any reason why the writing sin't working properly? i have resorted to calling this method from both the stop() and destroy() methods.

As a note, the load and saving both work perfectly when run from eclipse, but when in a JAR only the loading works, but nothing saves so I can't change the load data.

Ideally, I want this saveLocations() method to be called whenever the page is closed or refreshed.

NOTE: mOUtputStream is indeed a PrintWriter (it used to be an OutputStream, I guess I should change the name)

Thanks so much in advance for the help.


private void saveLocations() throws IOException {
        //JOptionPane.showMessageDialog(null, "Alert", "Saving", JOptionPane.INFORMATION_MESSAGE);
        // System.out.println("saving!");
        try {
            mOutputStream = new PrintWriter(new File(getClass().getResource("/listings/saveData.txt").toURI()));
        }
        catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //System.out.println(locations.size());
        for (Location l : locations) {
            System.out.println("r: " + l.getRawListing());
            mOutputStream.print(l.getRawListing()+ "\n");
        }


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

    }

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-11-04 23:37:25

您无法写入 JAR 文件内的文件——就这样。甚至不要尝试。如果您需要写入文件,那么该文件必须位于 JAR 之外。对于小程序,这需要对其进行签名,并要求用户授予特定权限才能执行此操作。

在小程序的情况下,我不确定您希望写入 JAR 文件的哪个副本:浏览器缓存中的副本,还是服务器上的副本?不管怎样,这都不会发生。

You can't write to a file inside a JAR file -- period. Don't even try. If you need to write to a file, then that file has to be outside the JAR. For an applet, that would require it to be signed, and to ask the user for specific permission to do so.

In the applet case, I'm not sure what copy of the JAR file you're hoping will be written to: the copy in the browser cache, or the copy on the server? Either way, it's not going to happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文