View RSS Feed

My Java Tips

Reading/writing files

Rate this Entry
by , 11-22-2011 at 05:26 PM (659 Views)
This 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.

Java 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.

Java Code:
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!!

Submit "Reading/writing files" to Facebook Submit "Reading/writing files" to Digg Submit "Reading/writing files" to del.icio.us Submit "Reading/writing files" to StumbleUpon Submit "Reading/writing files" to Google

Comments