Wednesday, September 2, 2009

Read Text File in Blackberry Mobile Device

public String readFile(String path)
{
//File path Should be given like> readFile("file:///SDCard/as.txt")
String str = null;
try
{
FileConnection fc = (FileConnection)Connector.open(path,Connector.READ);
if(!fc.exists())
{
Dialog.alert("File doesn't exist!");
}
else
{
int size = (int)fc.fileSize();
InputStream is = fc.openInputStream();
byte bytes[] = new byte[size];
is.read(bytes, 0, size);
str=new String(bytes);
}

}
catch (IOException ioe)
{
Dialog.alert(ioe.getMessage());
}
return str;
}

No comments:

Post a Comment