01-25-2013 08:31 PM - edited 01-25-2013 08:51 PM
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
Solved! Go to Solution.
01-26-2013 03:58 PM
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.
01-27-2013 01:19 PM
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
01-28-2013 05:28 AM
try to put the name like that
getResourceAsStream("holidays.xml");