02-23-2010 03:25 PM
Files created by a BB app on the SD Card can't be view with notepad in Windows.
I have an application which created a few text files on the SDCard. However, the files seems to be corrupted under windows, but the BB app can read the files fine and it gets back exactly what is written. Does the BB OS encrypts the files?
Here is the code
fconn = (FileConnection) Connector.open("file:///store/home/user/Config.doc ", Connector.READ_WRITE);
os = (DataOutputStream) fconn.openDataOutputStream();
writeString(os, "1234567890");
writeString(os, "abcdefg");
os.flush();
os.close();
fconn.close();
private void writeString(DataOutputStream os, String s)
{
try
{
os.writeInt(s.trim().length());
os.write(s.trim().getBytes());
}
catch (IOException e) {}
}
Solved! Go to Solution.
02-23-2010 04:08 PM
What do you see in Notepad? I have an idea of what you might be seeing but want to make sure.
02-23-2010 04:25 PM
Some garbage like this that can be display here. Again, if read it back to the BB app, I got what was written out.
02-23-2010 05:12 PM
Notepad doesn't do formatting so stuff like string length will be like garbage. Do you read it back in the same manner that you write it out? Such as:
private String readString(DataInputStream is)
{
try
{
byte[] bytes = new byte[is.readInt()];
is.readFully(bytes);
return new String(bytes);
}
catch (IOException e) {}
return "";
}If so then as stated before Notepad doesn't support formating so I would expect the String itself is fine but the int is coming up looking weird or at least has 4 spaces before the String. As a side note, try Wordpad, it supports formatting and might return the text correctly.
02-23-2010 05:39 PM
Have you tried opening the resultant file in a Hex editor? My suspicion is that it is being written in unicode and I think notepad expects the first two bytes of the file to tell it that the file is not in ANSI/ASCII format, otherwise it assumes it is ANSI and treats every byte as a character, rather than every two bytes as a character.
02-23-2010 06:53 PM
How are you reading back the data from the Blackberry?
If you weren't already aware, DataOutputStream writeInt is not going to write 42 as "42" into the file, it will write binary data ("Writes an int to the underlying output stream as four bytes, high byte first.").
What do you exactly mean by "can't be view with notepad in Windows"? What are you seeing in a hex editor from your sample code?
02-23-2010 07:09 PM
Perhaps you should change writeString to use either of the following:
DataOutputStream.writeUTF( String s) [Preferred]
DataOutputStream.writeChars(String s)
--MB
02-24-2010 08:28 AM - last edited on 02-24-2010 08:31 AM
If you're seeing a .rem extension appended to the file name, it means the file is DRMed and can only be decrypted by this BlackBerry. This may happen if Encryption is enabled for the Media Card (have a look at Options -> Security -> Encryption). Also, it's possible to programmatically steer whether files you create will be encrypted or not: if the FileConnection is an instance of ExtendedFileConnection you can then invoke various interesting methods on it.
02-24-2010 08:58 AM
Ah. No wonder.
Yes. There is a .rem extension. I have to check the security setting. Is the default security on? I never turn it on myself. Thanks guys.