2011-09-28 157 views

回答

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