简体   繁体   中英

Import Data into a Java program on different platforms?

I'm currently writing a java program that requires some Data to run.
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.

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. I do this on my Windows 7, 32 bit PC.

However, I wish for my program to be fully compatible on all platforms that can support Java, such as mobile devices.
Is there any special considerations I need to make in regards to file IO to ensure the program is compatible?

I have no idea how file IO works on platforms such as Android or iPads, or even Macs. How should I perform this data importation such that the program can still be run on all Java compatible platforms?

One possibility I've considered is to have these data types 'manually' embedded into the code. 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.

Thanks!


Specs:
Java SE (jre 7) Application.

Resources in J2SE, Android and (probably) J2ME that are 'embedded application resources' can (and thereby should) be accessed via. URL.

Obtain the URL using something like:

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() .

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.

As mentioned by gt_ebuddy, a CSV might be used for this. 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.

AFAIK, all the platforms have option to perform IO works. My suggestion in your case would be:

  1. The txt file is most portable/cross-platform and simple enough to read and parse. 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." :

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: for Java supported mobile devices
  • JavaSE: for desktop applications
  • Android: Dalvik VM supported

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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