【问题标题】:J2ME/Blackberry - how to read/write text file?J2ME/Blackberry - 如何读/写文本文件?
【发布时间】:2010-12-03 21:49:41
【问题描述】:

请给我一个在黑莓应用程序中读/写文本文件的示例代码。

【问题讨论】:

    标签: blackberry file-io java-me text-files jsr75


    【解决方案1】:

    我的代码 sn-p 用于字符串读/写文件:

    private String readTextFile(String fName) {
      String result = null;
      FileConnection fconn = null;
      DataInputStream is = null;
      try {
       fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
       is = fconn.openDataInputStream();
       byte[] data = IOUtilities.streamToBytes(is);
       result = new String(data);
      } catch (IOException e) {
       System.out.println(e.getMessage());
      } finally {
       try {
        if (null != is)
    
         is.close();
        if (null != fconn)
         fconn.close();
       } catch (IOException e) {
        System.out.println(e.getMessage());
       }
      }
      return result;
     }
    

    private void writeTextFile(String fName, String text) {
      DataOutputStream os = null;
      FileConnection fconn = null;
      try {
       fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
       if (!fconn.exists())
        fconn.create();
    
       os = fconn.openDataOutputStream();
       os.write(text.getBytes());
      } catch (IOException e) {
       System.out.println(e.getMessage());
      } finally {
       try {
        if (null != os)
         os.close();
        if (null != fconn)
         fconn.close();
       } catch (IOException e) {
        System.out.println(e.getMessage());
       }
      }
     }
    

    【讨论】:

    • 最好使用 Connector.READ_WRITE 而不是 Connector.WRITE(在我的情况下,第二个不起作用)。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 2012-09-15
      • 2019-06-04
      • 1970-01-01
      相关资源
      最近更新 更多