0

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.

Mahadi Siregar
  • 615
  • 3
  • 17
  • 38

1 Answers1

1

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);
Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
  • How about download xml file and save it under res folder? Do you have example or tutorial? – Mahadi Siregar Dec 04 '12 at 12:17
  • You can not save a file to the application package after it is installed on a device. You may save to the handset internal memory or sd card. – Telmo Pimentel Mota Dec 04 '12 at 20:00
  • About download and save file, I have search tutorial about it, but still not find. My plan is to read the file content, create new file and write the content to the new file. Is this a good way? – Mahadi Siregar Dec 05 '12 at 03:00
  • Yes, it is. Start finding a place to right that file. Use FileSystemRegistry.listRoots() to see which are available. – Telmo Pimentel Mota Dec 05 '12 at 12:24