如何将文本附加到黑莓中的文本文件中

时间:2011-09-28 04:59:52

标签: blackberry java-me

我想将文本附加到黑莓中的文本文件中,我该怎么做

1 个答案:

答案 0 :(得分:2)

查看此代码。这基本上将日志写入SD卡中的文件。您可以自定义您想要的方式。请享用。

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) {
                }
            }
            }
        }
        }