Reading/writing files
29 April 2008This post is all about reading and writing text files on mobile device.
In order to write files on the mobile device, you need com.motorola.file.writeaccess. If your application has that permission, you can write files easily using the following code.
FileConnection sc = (FileConnection)Connector.open("file:///phone/tmp.txt"); OutputStream os = sc.openOutputStream(); os.write(("text to go into the file").getBytes()); os.flush(); os.close();
com.motorola.file.readaccess is need to read files from mobile device. Files are read byte by byte as shown below.
FileConnection sc = (FileConnection)Connector.open("file:///phone/tmp.txt"); InputStream is = sc.openInputStream(); StringBuffer sofar = new StringBuffer(); byte c; while ((c = (byte)is.read()) != -1) { sofar.append((char)c); } is.close();
You may use this code to learn how to read and write from mobile device. Play with this and explore. Its simple and interesting.
Happy codding!!
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Reading/writing files