I recently worked on a J2ME application and had to read and write files to the filesystem. Now for reading files, most of the articles I found online told you how to read files that were in the 'res' folder of the application. But this method, for some reason, doesn't work for reading files from the file system. After much browsing, I finally found a way to to it. Here are the two functions,
//Function to read file in the 'res' folder of the application
private String readResourceFile(String filename) {
try {
Reader in = new InputStreamReader(this.getClass().getResourceAsStream(filename), "UTF8");
StringBuffer sbuffer = new StringBuffer(1024);
char[] buffer = new char[1024];
int chars, i = 0;
while ((chars = in.read(buffer, 0, buffer.length)) != -1) {
sbuffer.append(buffer, 0, chars);
}
return sbuffer.toString();
} catch (Exception e) {
return "Failed to read file.";
}
}
//Function to read file from filesystem
private String readFile(String filename) {
try {
FileConnection fc = (FileConnection)
Connector.open(filename, Connector.READ);
if(!fc.exists()) {
throw new IOException("File does not exist");
}
InputStream is = fc.openInputStream();
int ch;
StringBuffer sbuffer = new StringBuffer(1024);
while ((ch = is.read()) != -1) {
sbuffer.append((char)ch);
}
return sbuffer.toString();
} catch (Exception e) {
return "Failed to read file.";
}
}Now the parameter to the first function will be just the name of the file in the 'res' folder, while the parameter to the second function will be the complete path of the file, for example, call
readResourceFile('file.txt');
readFile('file:///E:/file.txt');
Also, readFile() only works for the files on the memory card. Trying to read files in 'C:' would lead to an error, I assume because of insufficient permissions.
Writing to file is quite straightforward, the only quirk being that again I could write to the memory card, but not to 'C:'.
//Function to write file to the filesystem. Example use, 'writeFile('file:///E:/file.txt')'
private boolean writeFile(byte[] data, String filename) {
Connection c = null;
OutputStream os = null;
try {
c = Connector.open(filename, Connector.READ_WRITE);
FileConnection fc = (FileConnection) c;
if (!fc.exists())
fc.create();
else
fc.truncate(0);
os = fc.openOutputStream(fc.fileSize());
os.write(data);
os.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (os != null)
os.close();
if (c != null)
c.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Also, if you want to download a large file over HTTP, using the HTTP library gives some problems. For large files, http.getLength() returns -1, even when the data has been downloaded. Using a buffered reader to read this data doesn't work well as well. The best way to do this is
//Download a large file over http
private String downloadFile() throws IOException {
StreamConnection c = null;
InputStream s = null;
try {
c = (StreamConnection)Connector.open(url);
s = c.openInputStream();
int ch;
StringBuffer sbuffer = new StringBuffer(1024);
while ((ch = s.read()) != -1) {
sbuffer.append((char)ch);
}
return sbuffer.toString();
} finally {
if (s != null)
s.close();
if (c != null)
c.close();
}
}
Accessing the filesystem was the most frustrating part of coding with coding with J2ME, the rest is fun. Have fun with it.
Sunday, September 5, 2010
Saturday, November 17, 2007
friend
you come across certain people in life talking to whom seems to liberate you from the vicious circle of life.. if there is someone to whom you can just talk and be yourself, without the fear of being judged , without the fear of being hated, without the fear of being misunderstood, its the best feeling on earth.. everyone has secret thoughts, secret wishes, not to be shared with anyone.. but for once, try to be totally honest with somebody, bare all your thoughts, all your wishes, stand totally naked(mentally ;) ), and let yourself be probed to your depths.. and if all this doesn't discomfort you, you have found yourself a friend for a lifetime..
at the end of the day, everyone wants to be acknowledged, to be remembered, and it sure means a lot if you are remembered by someone who really cares..
at the end of the day, everyone wants to be acknowledged, to be remembered, and it sure means a lot if you are remembered by someone who really cares..
Wednesday, November 14, 2007
music
its weird that no matter how lonely, how individual, how detached you feel from the world, you will always find a song that portrays just how you feel.. makes me wonder if the world is really all that chaotic, or are we all tied together by a string of "fate".. music is just so amazing.. it can make you feel sad when you are happy, it can make you smile when you are down, it can make you relax, it can make you dance, it can make you sing along.. is music just a stream of sounds, or is it more.. is it an expression of our race, or of an individual.. i guess great music is one thing that machines, no matter how advanced, will ever be able to produce..
i guess i am making no sense at all, but then, that's the reason this blog's been named "line of thought"..
i guess i am making no sense at all, but then, that's the reason this blog's been named "line of thought"..
Sunday, October 14, 2007
not all ministers are bad
Statutory warning:
this is the the first ever post of my first ever blog.. so read on if you have some time to kill.. ;)
so here i am at the microsoft techvista 2007.. the theme is 10 years into the future.. there are top computer scientists from around the world, talking about their work and research.. now i am completely mesmerized by these people and am thinking about the technicalities if their work, the probable difficulties, their solutions.. and here comes this guy, mr. kabil sibal, union science and technology minister, starts casually, and slowly and slowly relates all that was said to the problems india is facing and how these researches can be helpful in solving them.. he also thinks up of some truly beautiful ideas that can become good research material and hands them out to the microsoft people, who are as impressed as i am.. all the time i was listening to the previous speakers and analyzing their work, he was taking it all in and relating it to the problems at hand, thats focus for you.. and remember this is a guy from non-science college background, and he looks so comfortable and confident among these great scientists.. and everyone admires him.. thats i believe the power of thinking analytically and at the same time, keeping the complete picture in your mind.. thats something not everyone is capable of and thats what makes him a really good minister.. good work mr. prime minister, you selected some great guys to work for you
this is the the first ever post of my first ever blog.. so read on if you have some time to kill.. ;)
so here i am at the microsoft techvista 2007.. the theme is 10 years into the future.. there are top computer scientists from around the world, talking about their work and research.. now i am completely mesmerized by these people and am thinking about the technicalities if their work, the probable difficulties, their solutions.. and here comes this guy, mr. kabil sibal, union science and technology minister, starts casually, and slowly and slowly relates all that was said to the problems india is facing and how these researches can be helpful in solving them.. he also thinks up of some truly beautiful ideas that can become good research material and hands them out to the microsoft people, who are as impressed as i am.. all the time i was listening to the previous speakers and analyzing their work, he was taking it all in and relating it to the problems at hand, thats focus for you.. and remember this is a guy from non-science college background, and he looks so comfortable and confident among these great scientists.. and everyone admires him.. thats i believe the power of thinking analytically and at the same time, keeping the complete picture in your mind.. thats something not everyone is capable of and thats what makes him a really good minister.. good work mr. prime minister, you selected some great guys to work for you
Subscribe to:
Comments (Atom)