本文介绍了将数据附加到.jar中的Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要发布一些具有相关数据集的Java代码。它是设备的模拟器,我希望能够在一个.JAR文件中包含用于模拟记录的所有数据。在这种情况下,每个模拟记录包含四个字段(主叫方,被叫方,呼叫开始,呼叫持续时间)。

I need to ship some Java code that has an associated set of data. It's a simulator for a device, and I want to be able to include all of the data used for the simulated records in the one .JAR file. In this case, each simulated record contains four fields (calling party, called party, start of call, call duration).

最好的方法是什么?我已经沿着生成数据的路径走向了Java语句,但是IntelliJ似乎并不特别高兴处理100,000行Java源文件!

What's the best way to do that? I've gone down the path of generating the data as Java statements, but IntelliJ doesn't seem particularly happy dealing with a 100,000 line Java source file!

是否有更聪明的方法吗?

Is there a smarter way to do this?

在C#/ .NET世界中,我将数据创建为单独的文件,将其作为资源嵌入到程序集中,然后使用反射在运行时将其拉出并访问它。我不确定Java世界中适当的比喻是什么。

In the C#/.NET world I'd create the data as a separate file, embed it in the assembly as a resource, and then use reflection to pull that out at runtime and access it. I'm unsure of what the appropriate analogy is in the Java world.

FWIW,Java 1.6,适用于Solaris。

FWIW, Java 1.6, shipping for Solaris.

推荐答案

在JAR中包含静态资源文件是完全可以的。这通常用属性文件完成。您可以使用以下内容访问该资源:

It is perfectly OK to include static resource files in the JAR. This is commonly done with properties files. You can access the resource with the following:

  Class.getResourceAsStream ("/some/pkg/resource.properties");

其中/是相对于类路径的根目录。

Where / is relative to the root of the classpath.

本文讨论主题。

这篇关于将数据附加到.jar中的Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 07:12