Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Aviator168
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon
Accepted Solution

Why my text written to a file on the SD card seems to be corrupted

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) {}
    }


 

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1154, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Why my text written to a file on the SD card seems to be corrupted

What do you see in Notepad? I have an idea of what you might be seeing but want to make sure.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
Aviator168
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: Why my text written to a file on the SD card seems to be corrupted

Some garbage like this that can be display here. Again, if read it back to the BB app, I got what was written out.

 

Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.3253, Z10 (STL100-3)-10.3.1.634 Dev OS, Z30 (STA100-5)-10.3.1.634 Dev OS, Passport (SQW100-1)-10.3.0.1154, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Why my text written to a file on the SD card seems to be corrupted

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.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
GlenBirkbeck
Posts: 54
Registered: ‎10-23-2008
My Device: Not Specified

Re: Why my text written to a file on the SD card seems to be corrupted

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.

Please use plain text.
Developer
ydaraishy
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Why my text written to a file on the SD card seems to be corrupted

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?

Please use plain text.
Developer
Developer
mb1
Posts: 320
Registered: ‎05-26-2009
My Device: Not Specified

Re: Why my text written to a file on the SD card seems to be corrupted

Perhaps you should change writeString to use either of the following:

 

DataOutputStream.writeUTF( String s) [Preferred]

DataOutputStream.writeChars(String s)

 

--MB

Please use plain text.
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Why my text written to a file on the SD card seems to be corrupted

[ Edited ]

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.

Please use plain text.
Developer
Aviator168
Posts: 709
Registered: ‎09-10-2009
My Device: 8520, 8900, 9000, 9300, 9650, 9700, 9780, 9800, 9810, 9900, 9930
My Carrier: Verizon

Re: Why my text written to a file on the SD card seems to be corrupted

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.

Please use plain text.