Reading/writing files

29 April 2008

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.

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!!

del.icio.us:Reading/writing files  digg:Reading/writing files  spurl:Reading/writing files  wists:Reading/writing files  simpy:Reading/writing files  newsvine:Reading/writing files  blinklist:Reading/writing files  furl:Reading/writing files  reddit:Reading/writing files  fark:Reading/writing files  blogmarks:Reading/writing files  Y!:Reading/writing files  smarking:Reading/writing files  magnolia:Reading/writing files  segnalo:Reading/writing files  gifttagging:Reading/writing files

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

Leave a Reply