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
Highlighted
Contributor
Posts: 49
Registered: ‎05-02-2012
My Device: Bold 9790
My Carrier: Wind Mobile
Accepted Solution

Copy file in 'res' to file system

[ Edited ]

Hi there,

 

I'm trying to copy a file from my .cod file into the file system so I can overwrite it etc. However, I can't seem to get the file to write. This is the code I'm using right now, I found it when searching the community.

 

try {
            FileConnection fc = (FileConnection) Connector.open("file:///store/home/holidays.xml");
            if (!fc.exists()) {
                fc.create();
                OutputStream os = fc.openOutputStream();
                InputStream is = getClass().getResourceAsStream("/xml/holidays.xml");
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = is.read(buf)) > 0) {
                    os.write(buf, 0, len);
                }
                is.close();
                os.close();
                add(new LabelField("Wrote file."));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

 

This is in the constructor for the screen. When I run the app, I dont get "Wrote file.", just a blank screen. Any suggestions on what I'm doing wrong?

 

Thank you in advance,

Ben

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Copy file in 'res' to file system

Hi Ben,

 

Use a debugger to see which line is throwing an error.

 

Once you will have that, it will be easier to find a solution.

 

Also, are you sure that:

 

1. The file file:///store/home/holidays.xml doesn't already exist?

 

2. The path/xml/holidays.xml is the correct path for the resource file?

 

Hope that helps,

 

E.

Contributor
Posts: 49
Registered: ‎05-02-2012
My Device: Bold 9790
My Carrier: Wind Mobile

Re: Copy file in 'res' to file system

Thanks for the advice,

 

I finally figured out what was going on; something about reading the xml/holidays.xml file wasn't working at all. In the end I read from a holidays.txt file to an xml file in the file system. Its not a good solution but it works fine now.

 

Thanks,

Ben

Regular Contributor
Posts: 81
Registered: ‎01-16-2012
My Device: BOLD 9650 + developer
My Carrier: Vodafone

Re: Copy file in 'res' to file system

try to put the name  like that 
getResourceAsStream(
"holidays.xml");