08-13-2008 12:19 PM
Hello,
I was wondering how I can open a .txt file on a memory card so that I can read the content?
The file is at the path: file:///testfile.txt
Thank you for any help.
Solved! Go to Solution.
08-14-2008 09:14 AM
You can open an input stream from the file using:
InputStream inputStream = null;
FileConnection fileConnection = (FileConnection) Connector.open( "file:///SDCard/testfile.txt");
if (fileConnection.exists()) {
inputStream = fconn.openInputStream();
}
To get the input stream as text you can read it to a ByteArrayOutputStream and create a new String from the bytes.
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; while (true) { int bytesRead = inputStream.read( buffer, 0, BUFFER_SIZE ); if (bytesRead == -1) break; byteArrayOutputStream.write( buffer, 0, bytesRead ); } byteArrayOutputStream.flush(); byte[] result = byteArrayOutputStream.toByteArray(); byteArrayOutputStream.close(); String resultString = new String(result);
12-11-2009 09:40 AM
Hello,
Is it necessary to sign code if you want to read and write content into a file.txt in the memory card?
12-11-2009 09:50 AM
No. It does not require code signing.
But to be sure check http://www.blackberry.com/developers/docs/4.7.0api