Search code examples
blackberryjde

writing to text file in blackberry


is there any way out that we can make the data in text file persistent? everytime a user finishes playing a game, in my program his name and respective score is written to a text file. When the next player comes the previous one gets overwritten. since am writing in write mode, I am not sure whether append mode is supported to save scores of this sort in blackberry...any suggestions are welcome


Solution

  • You should really use the PersistentStore to store this type of information - it's much easier to use and probably more reliable than trying to write files.

    However, if you insist on writing files, here's the general code to open a file for appending:

    private OutputStream openFileForWriting(String filePath) {
        try {
            FileConnection fconn = (FileConnection) Connector.open(filePath);
            // If no exception is thrown, then the URI is valid, but the file may or may not exist.
            if (!fconn.exists()) {
                fconn.create();  // create the file if it doesn't exist
            }
            return fconn.openOutputStream(fconn.fileSize());
        } catch (IOException ioe) {
            System.out.println("Could not open " + filePath + " for writing");
        }
        return null;
    }