如何在blackberry中本地保存xml文件?
我想从互联网上获取xml,但每次从网络获取xml都会使我的应用程序速度变慢,所以我想获取并保存在本地目录中,下次打开应用程序时,在后端将xml再次从互联网复制到我的xml如何在blackberry中本地保存xml文件?,xml,blackberry,Xml,Blackberry,我想从互联网上获取xml,但每次从网络获取xml都会使我的应用程序速度变慢,所以我想获取并保存在本地目录中,下次打开应用程序时,在后端将xml再次从互联网复制到我的xml 如何做到这一点,还有没有其他好办法解决这个问题?请使用以下函数将文件写入SD卡 private static String APP_DOC_DIR = "file:///SDCard/BlackBerry/documents/MyAPP/"; public static void writeToSD(String
如何做到这一点,还有没有其他好办法解决这个问题?请使用以下函数将文件写入SD卡
private static String APP_DOC_DIR = "file:///SDCard/BlackBerry/documents/MyAPP/";
public static void writeToSD(String fileName, String fileContent){
FileConnection fconn = null;
// APP_DOC_DIR is the directory name constant.
try {
FileConnection fc = (FileConnection)Connector.open(APP_DOC_DIR);
// If no exception is thrown, the URI is valid but the folder may not exist.
if (!fc.exists())
{
fc.mkdir(); // create the folder if it doesn't exist
}
fc.close();
fconn = (FileConnection) Connector.open(APP_DOC_DIR + fileName ,Connector.READ_WRITE);
if (!fconn.exists()) {
fconn.create();
}
fconn.setReadable(true);
fconn.setWritable(true);
OutputStream os = fconn.openOutputStream();
os.write(fileContent.getBytes("UTF8"));
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fconn!=null) {
try {
fconn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
从SD卡读取的文件如下
public static String readFromSD(String fileName)
{
String resultString = "";
int BUFFER_SIZE = 10000;
InputStream inputStream = null;
FileConnection fconn;
try {
fconn = (FileConnection) Connector.open( APP_DOC_DIR + fileName, Connector.READ);
if (fconn.exists())
{
inputStream = fconn.openInputStream();
}
else
{
return "";
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[BUFFER_SIZE];
while (true) {
int bytesRead = inputStream.read( buffer, 0, BUFFER_SIZE );
if (bytesRead == -1)
break;
byteArrayOutputStream.write( buffer, 0, bytesRead );
}
byteArrayOutputStream.flush();
byte[] result = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
//resultString = new String(result,"UTF8");
resultString = new String(result);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resultString;
}
没有应用程序文件夹吗?xml的使用是强制性的吗?虽然我没有测试它,但至少有人敢于思考和回答,谢谢你,先生。。。