简体   繁体   English

将txt文件添加到可运行的JAR文件

[英]Add txt files to a runnable JAR file

I'm trying to make a runnable jar file and I'm having problems with my .txt files. 我正在尝试制作一个可运行的jar文件,而我的.txt文件遇到了问题。

My program also have images, but fortunately I've figured out how to manage them. 我的程序也有图像,但是幸运的是我已经弄清楚了如何管理它们。 I'm using something like this with them and it works just fine both Eclipse and the jar: 我在他们身上使用了类似的东西,并且在Eclipse和jar上都可以正常工作:

logoLabel.setIcon(new ImageIcon(getClass().getResource("/logo.png")));

My problem is when I've something like this in one of my classes: 我的问题是当我在一个班级上遇到类似这样的事情时:

try {
    employeeList =  (TreeSet<Employee>) ListManager.readFile("list/employeeList.txt");
} catch (ClassNotFoundException i) {
    i.printStackTrace();
} catch (IOException i) {
    i.printStackTrace();
}

And this in the class ListManager that I use to read my lists serialized in the .txt files: 这是我用来读取在.txt文件中序列化的列表的ListManager类中的代码:

public static Object readFile(String file) throws IOException, ClassNotFoundException {
    ObjectInputStream is = new ObjectInputStream(new FileInputStream(file));
    Object o = is.readObject();
    is.close();
    return o;
}

I also have a similar method to write in the files. 我也有类似的方法来写入文件。

I've tried several combinations that I've found here: 我尝试了几种在这里找到的组合:

How to include text files with Executable Jar 如何在可执行Jar中包含文本文件

Creating Runnable Jar with external files included 创建包含外部文件的可运行Jar

Including a text file inside a jar file and reading it 在jar文件中包含一个文本文件并读取它

I've also tried with slash, without slash, using openStream, not using openStream... But or I get a NullPointerException or it doesn't compile at all... 我也尝试过使用斜杠(不使用斜杠),使用openStream而不使用openStream ...,但是或者我得到了NullPointerException或根本无法编译...

Maybe is something silly or maybe is a concept error that I've of how URL class works, I'm new to programming... 也许是愚蠢的事情,或者是概念错误,这是我关于URL类的工作方式的问题,我是编程的新手...

Thank you very much in advance for your advice! 预先非常感谢您的建议!

EDIT: 编辑:

It's me again... The answer Raniz gave was just what I needed and it worked perfect, but now my problem is with the method that I use to write in the files... 再次是我... Raniz给出的答案正是我所需要的,而且效果很好,但是现在我的问题是我用来写入文件的方法...

public static void writeFile(Object o, String file) throws IOException {
    ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(file));
    os.writeObject(o);
    os.close();
}

try {
   ListManager.writeFile(employeeList.getEmployeeList(), "lists/employeeList.txt");
} catch (IOException i) {
   i.printStackTrace();
}

Could you help me please? 请问你能帮帮我吗? I don't know what I should use to replace FileOutputStream, because I think there is the problem again, am I right? 我不知道应该用什么来替换FileOutputStream,因为我认为仍然存在问题,对吗?

Thank you very much! 非常感谢你!

Yes, if you want to read resources from inside a jar file, you shouldn't use FileInputStream . 是的,如果您想从jar文件中读取资源,则不应使用FileInputStream Perhaps you should add a readResource method: 也许您应该添加一个readResource方法:

public static Object readResource(Class clazz, String resource)
    throws IOException, ClassNotFoundException {
  try (ObjectInputStream is =
           new ObjectInputStream(clazz.getResourceAsStream(resource))) {
     return is.readObject();
  }
}

(I'd also suggest updating your readFile method to use a try-with-resources block - currently if there's an exception you won't close the stream...) (我还建议您更新readFile方法以使用try-with-resources块-当前,如果有异常,您将无法关闭流...)

Note that when you say "I also have a similar method to write in the files" - you won't be able to easily write to a resource in the jar file. 请注意,当您说“我也有类似的写入文件的方法”时,您将无法轻松地将其写入jar文件中的资源。

The problem is that you're trying to access a file inside of a JAR archive as a file in the file system (because that's what FileInputStream is for) and that won't work. 问题是您正在尝试将JAR存档内的文件作为文件系统中的文件进行访问(因为这就是FileInputStream目的),并且无法正常工作。

You can convert readFile to use an URL instead and let URL handle opening the stream for you: 您可以将readFile转换为使用URL,然后让URL为您打开流:

public static Object readFile(URL url) throws IOException, ClassNotFoundException {
    ObjectInputStream is = new ObjectInputStream(url.openStream());
    Object o = is.readObject();
    is.close();
    return o;
}

You should also put your code in a try-statement since it currently doesn't close the streams if an IOException occurs: 您还应该将代码置于try语句中,因为如果发生IOException,当前代码不会关闭流:

public static Object readFile(URL url) throws IOException, ClassNotFoundException {
    try(ObjectInputStream is = new ObjectInputStream(url.openStream())) {
        Object o = is.readObject();
        return o;
    }
}

try {
    employeeList =  (TreeSet<Employee>) ListManager.readFile(getClass().getResource("/list/employeeList.txt"));
} catch (ClassNotFoundException i) {
    i.printStackTrace();
} catch (IOException i) {
    i.printStackTrace();
}

I also have a similar method to write in the files. 我也有类似的方法来写入文件。

That won't work if the files are inside the JAR so you should probably consider having your files outside your JAR. 如果文件位于JAR内,则无法使用,因此您可能应该考虑将文件置于JAR之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM