我想编辑我的问题。
如果文件仍然不存在,则下载并保存它,我案例。为了检查文件是否存在,我尝试使用下面的代码
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;
}
}在我的资源类中
public static final String XML_PATH = "/xml";
public static final String XML_FILENAME = "template.xml";我在res文件夹下创建了'xml‘文件夹,并在那里放置了文件'template.xml’。但是这段代码总是返回false。我们是否不能使用res文件夹?那么,正确的路径是什么呢?
发布于 2012-12-04 19:53:35
res文件夹下的文件始终可用,因为它们与类文件打包在一起。因此,如果它们存在,您不需要向FileConnection进行检查。
要访问这些文件,可以使用Class.getResourceAsStream。
例如:
private InputStream open (String fileName) {
return getClass().getResourceAsStream(fileName);
}
// call it like this
open(Resource.XML_PATH + "/" + Resource.XML_FILENAME);https://stackoverflow.com/questions/13696224
复制相似问题