Local File I/O - is it possible?

There is probably a simple answer to this problem, but I’ll ask anyway.

Is there any way to do local file i/o in J2ME? Specifically, I am wanting to access a data file packaged in my JAR file. J2ME does not support Properties or ResourceBundles, and I tried to do:

DataInputStream dis = Connector.openDataInputStream(“file://xyz”);

… but it gave me a non-supported protocol exception.

Thanks.

Off the top of my head, I think it’s device/manufacturer specific. A manufacturer can offer other connectors but is under no obligation to do so. So even if you had that facility, you might not want to use it if you’re trying to target a number of phones.

That said, if you’re trying to get something out of the jar, maybe you can call

DataInputStream dis = new DataInputStream(this.getClass().getResourceAsStream(“myresource”));

But I can’t recall ever doing this, so hopefully someone else can confirm.

yeah, the getResourceAsStream() works fine for reading from the jar.

As for storage, that is what the rms package is for.

Some manufacturers do provide ways to write directly to files, but as said earlier, it makes your code platform dependant.

Siemens and some motorola (e.g. i730) provide file access. i believe some japanese phone (iAppli…) has file access as well. otherwise, nothing in the standard spec

ben

JasonB is right: in the special case of reading a file in your MIDlet’s JAR, you should use getResourceAsStream. It’s actually the same thing as when you use Image.createImage("/myfile.png") - that’s also reading a file from the JAR, and is typically implemented using getResourceAsStream.

As with Image.createImage, you probably want to start the file name with “/”, otherwise it’ll look for the resource in a directory based on the class’s package name.

If you want to read some other local file in the phone, you’ll have to wait for phones which implement the JSR-75 File API, or use some proprietary API if the phone you target has one.

Great. Thanks everyone. Never thought about using getResourceAsStream(). I’ll give it a try.

check out the oct articl eon wireless.java.com you can do file acces through gcf…ie access memory cards and thelike…