如何在jar中读/写文件 [英] How to read/write file in jar

查看:212
本文介绍了如何在jar中读/写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:我有一个jar,这个jar里面是一个文件。在此文件中写入的对象表示应用程序的最后一个状态(以便用户可以在他停止的位置拾取)。我在orde中使用java Serializable 接口来编写和读取Object。

Situation: I have a jar, and inside this jar is an file. In this file is an object written that represents the last state of the application (so that the user can pick up where he stopped). I'm using the java Serializable interface in orde to write and read the Object.

我的问题是:如何从jar运行应用程序时我是否读取了该文件。

My question is: how do I read the file when running the application from the jar.

代码到目前为止:

in = new ObjectInputStream(Configuration.class.getResourceAsStream("config.txt"));

Object o = in.readObject();
if(o instanceof Configuration)
    this.configuration = (Configuration) o;

只要我从eclips运行应用程序,这就完美了。
当我运行jar时,会创建一个窗口,但不会显示任何内容。所以我假设发生了一个错误。有人知道如何阅读文件吗?

This works perfect, as long as I'm running the application from eclips. When I run the jar, a window is created but nothing is displayed. So I assume an error occured. Does someone know how to read the file?

推荐答案

首选项API 可能是您尝试实现的更好选择。

The Preferences API might be a better option for what you're trying to achieve.

这篇关于如何在jar中读/写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆