简体   繁体   English

将数据导入不同平台上的 Java 程序?

[英]Import Data into a Java program on different platforms?

I'm currently writing a java program that requires some Data to run.我目前正在编写一个需要一些数据才能运行的 java 程序。
The data isn't particularly extensive;数据不是特别广泛; it's a list of all elements of the periodic table (in both symbol and full name) verse their atomic mass, up to about 7 decimal places each.它是元素周期表中所有元素(包括符号和全名)及其原子质量的列表,每个元素最多约 7 位小数。

Currently, I have this data stored in a.txt file on my computer in the directory of my Java program, and I intend to simply read it and manipulate it into my intended data types.目前,我将这些数据存储在我计算机上 Java 程序目录中的一个.txt 文件中,我打算简单地读取它并将其操作成我想要的数据类型。 I do this on my Windows 7, 32 bit PC.我在我的 Windows 7、32 位 PC 上执行此操作。

However, I wish for my program to be fully compatible on all platforms that can support Java, such as mobile devices.但是,我希望我的程序能够完全兼容所有可以支持 Java 的平台,例如移动设备。
Is there any special considerations I need to make in regards to file IO to ensure the program is compatible?关于文件 IO 是否需要特别注意以确保程序兼容?

I have no idea how file IO works on platforms such as Android or iPads, or even Macs.我不知道文件 IO 如何在 Android 或 iPad,甚至 Mac 等平台上工作。 How should I perform this data importation such that the program can still be run on all Java compatible platforms?我应该如何执行此数据导入,以便该程序仍然可以在所有 Java 兼容平台上运行?

One possibility I've considered is to have these data types 'manually' embedded into the code.我考虑过的一种可能性是将这些数据类型“手动”嵌入到代码中。 Ie; IE; I do not import the Data from file but instead code it straight into the program (which would make the Data unalterable after compilation, which I feel is bad practise).我不从文件中导入数据,而是直接将其编码到程序中(这会使数据在编译后不可更改,我认为这是不好的做法)。
I've also wondered whether I should be using Java ME instead of SE too.我也想知道我是否也应该使用 Java ME 而不是 SE。

Thanks!谢谢!


Specs:眼镜:
Java SE (jre 7) Application. Java SE (jre 7) 申请。

Resources in J2SE, Android and (probably) J2ME that are 'embedded application resources' can (and thereby should) be accessed via. J2SE 中的资源,Android 和(可能)J2ME 中的“嵌入式应用程序资源”可以(因此应该)通过访问。 URL. URL。

Obtain the URL using something like:使用类似以下内容获取 URL:

URL periodicTableData = this.getClass().getResource(
    "/relative/path/to/periodicTable.dat");

I assume there are different installation methods for different platforms?我假设不同平台有不同的安装方法?

'Static' resources, ones that don't change, can be added to a Jar. If that Jar is on the run-time class-path of the app., they will be found using getResource() . “静态”资源,即不会改变的资源,可以添加到 Jar。如果该 Jar 在应用程序的运行时类路径上,则可以使用getResource()找到它们。

The data.. (is).. a list of all elements of the periodic table (in both symbol and full name) verse their atomic mass, up to about 7 decimal places each.数据..(是).. 元素周期表中所有元素(包括符号和全名)及其原子质量的列表,每个元素最多约 7 位小数。

As mentioned by gt_ebuddy, a CSV might be used for this.正如 gt_ebuddy 所提到的,CSV 可能用于此目的。 OTOH I would tend to use XML. XML can handle more complicated structures than immediately needed here, but does offer the advantage of good inbuilt support (on J2SE at least), and (encoded in UTF-8) will be readable across platforms. OTOH 我倾向于使用 XML。XML 可以处理比此处立即需要的结构更复杂的结构,但确实提供了良好的内置支持(至少在 J2SE 上)的优势,并且(以 UTF-8 编码)可以跨平台读取。

AFAIK, all the platforms have option to perform IO works.据我所知,所有平台都可以选择执行 IO 作品。 My suggestion in your case would be:我对你的建议是:

  1. The txt file is most portable/cross-platform and simple enough to read and parse. txt 文件是最便携/跨平台的,并且足够简单,易于阅读和解析。 Use Text file everywhere.到处使用文本文件。
  2. To make parsing easier by the program in each platform, format your data file into tab separated or comma separated etc.为了让每个平台的程序更容易解析,请将数据文件格式化为制表符分隔或逗号分隔等。

"I've also wondered whether I should be using Java ME instead of SE too." “我也想知道我是否也应该使用 Java ME 而不是 SE。” : :

You have to code differently for each platform.您必须为每个平台编写不同的代码。 Though, there will be very few changes in your core logic (except GUI) across JavaME-JavaSE-Android.不过,跨 JavaME-JavaSE-Android 的核心逻辑(GUI 除外)几乎没有变化。

  • JavaME: for Java supported mobile devices JavaME:适用于 Java 支持的移动设备
  • JavaSE: for desktop applications JavaSE:用于桌面应用程序
  • Android: Dalvik VM supported Android:支持 Dalvik 虚拟机

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

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