views:

108

answers:

3

The class java.io.FileReader not found in Java ME.

I need this in order to get the file and then parse it with an xml parser.

Anyone know any alternatives for this class?

*added using CLDC profile. The xml file to be read is in the JAR.

+1  A: 

That's because Java ME provides only a limited subset of the java.io package. You need to use the java.microedition.io package instead.

For actual file I/O you'll need to use the FileConnection class provided by JSR-75.

Joachim Sauer
+1  A: 

What Java ME profile are you using? The CLDC does not support the concept of files at all.

In general, FileReader is nothing but a convenience class that wraps an InputStreamReader areound a FileInputStream. It's also very broken because it does not allow specifying the encoding, and should therefore almost never be used.

It would be especially wrong to use it to read XML because proper XML data specifies its encoding, and a proper XML parser will handle that, so you really should pass binary data to the XML parser.

So if you're on the CDC profile, just use a FileInputStream directly.

Michael Borgwardt
I'm using CLDC. I'm using FileReader since it is listed in the MiniXPath Demo for XPath for JavaME. And was wondering why it isnt present.
cancelledout
+1  A: 

Hi, the question is a bit ambiguous. I think Joachim's answer might be only partial if you are trying to read a local file. I'm certainly not sure though.

If the file is stored as a resource in your JAR, you can access it through the getResourceAsStream method in Class.

If the file is a local file on the file system and if I recall correctly, you need JSR-75 support. Over at Sun's developer page there is an introduction to JSR 75 and the fileconnection API.

Mikael Ohlson
The file is actually stored on JAR. I tried getResourceAsStream but java.lang.class is not found on Java ME. :( How do I go around this?
cancelledout
@cancelledout: what code are you using for that? It should definitely work according to the CLDC API. What class is not found?
Michael Borgwardt
Thanks so much for your reply.InputStream is = Myclassname.class.getResourceAsStream(file);This returns null.
cancelledout
And `file` in this case corresponds to the resource id?This how-to that I found may help: http://www.java-tips.org/java-me-tips/midp/how-to-read-a-resource-from-a-jar-file.htmlI haven't done any of this in a long, long time, so no guarantees.. :)
Mikael Ohlson