J2ME/Blackberry - how to read/write text file?

前端 未结 2 1077
梦谈多话
梦谈多话 2020-12-05 06:10

please give me a sample code for read/write text file in blackberry application.

相关标签:
2条回答
  • 2020-12-05 06:25

    Using

    FileConnection Interface

    0 讨论(0)
  • 2020-12-05 06:30

    My code snippet for string read/write files:

    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());
       }
      }
     }
    
    0 讨论(0)
提交回复
热议问题