I want to ap开发者_Python百科pend the text to a text file in blackberry ,how can i do it
Checkout this code. This basically writes log to a file in SD Card. You can customize the way you want. Enjoy.
static String pFilePath = "SDCard/BlackBerry/pictures/Log.txt";
public static void writeLog(String data) {
FileConnection fc = null;
OutputStream lStream = null;
Date d = new Date();
Calendar c = Calendar.getInstance();
String time = new String();
if (pFilePath != null) {
try {
fc = (FileConnection) Connector.open("file:///" + pFilePath,
Connector.READ_WRITE);
if (null == fc || fc.exists() == false) {
fc.create();
}
lStream = fc.openOutputStream(fc.fileSize());
c.setTime(d);
time = c.get(Calendar.HOUR_OF_DAY) + ":" + c.get(Calendar.MINUTE) + ":" + c.get(Calendar.SECOND) + ":" + c.get(Calendar.MILLISECOND);
data = "\n" + time + "("
+ (System.currentTimeMillis() - currentTimeMillies)
+ " ms)" + " -- " + data;
currentTimeMillies = System.currentTimeMillis();
lStream.write(data.getBytes());
} catch (Exception ioex) {
ioex.printStackTrace();
} finally {
if (lStream != null) {
try {
lStream.close();
lStream = null;
} catch (Exception ioex) {
}
}
if (fc != null) {
try {
fc.close();
fc = null;
} catch (Exception ioex) {
}
}
}
}
}
精彩评论