Question

I want to edit my question.

If file still not exist, download and save it, that my case. To check if file exist, I've try by this code

private boolean isXmlFileExist(){
    try{
        FileConnection fCon = (FileConnection) Connector.open("file:///"+Resource.XML_PATH + "/" + Resource.XML_FILENAME, Connector.READ);
        return fCon.exists();
    }catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

In my resource class

public static final String XML_PATH = "/xml";
public static final String XML_FILENAME = "template.xml";

I have create 'xml' folder under res folder and i have put file 'template.xml' there. But this code always return false. Whether we can not use the res folder? So, what is the correct path.

Was it helpful?

Solution

Files under res folder are always available because they are packaged with the class files. So, you do not need to check with FileConnection if they exist.
To access these files you can use Class.getResourceAsStream.
For example:

private InputStream open (String fileName) {
  return getClass().getResourceAsStream(fileName);
}

// call it like this
open(Resource.XML_PATH + "/" + Resource.XML_FILENAME);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top