J2ME files - how?

L

Lasse Laegteskov

Hi,

I'm writing a program for mobile phones and I have a bit of a hard time to
figure out _if_ I can read a small configuration file (text only) from the
jar-archive.

Can this be done in MIDP 1.0? And if it can, please give a small code
example...

Regards,

Lasse
 
J

JScoobyCed

Lasse Laegteskov said:
Hi,

I'm writing a program for mobile phones and I have a bit of a hard time to
figure out _if_ I can read a small configuration file (text only) from the
jar-archive.

Can this be done in MIDP 1.0? And if it can, please give a small code
example...

Umm... I have not tested, for text file, but I did for picture. Use the
getResourceAsStream(String) from the Class Object
<sniplet>
// in the class that extends MIDlet
public String getFile(String file) throws Exception {
InputStream is = this.getClass().getResourceAsStream(file);
int len=0;
byte[] data = new byte[64];
StringBuffer sb = new StringBuffer();
while((len=is.read(data))!=-1) {
sb.append(new String(data,0,len));
}
is.close();
return sb.toString();
}
</sniplet>

JScoobyCed
-------------
 
H

Hugh Beyer

Lasse Laegteskov said:
Hi,

I'm writing a program for mobile phones and I have a bit of a hard time to
figure out _if_ I can read a small configuration file (text only) from the
jar-archive.

Can this be done in MIDP 1.0? And if it can, please give a small code
example...

Umm... I have not tested, for text file, but I did for picture. Use the
getResourceAsStream(String) from the Class Object
<sniplet>
// in the class that extends MIDlet
public String getFile(String file) throws Exception {
InputStream is = this.getClass().getResourceAsStream(file);
int len=0;
byte[] data = new byte[64];
StringBuffer sb = new StringBuffer();
while((len=is.read(data))!=-1) {
sb.append(new String(data,0,len));
}
is.close();
return sb.toString();
}
</sniplet>

I've done this for text files and it works.

Watch out that the resource name includes the whole package hierarchy, e.g.:
"/com/company/app/data/config.txt"

Hugh
 
L

Lasse Laegteskov

Thank you for your answer! I will try it immidiatly!


Hugh Beyer said:
Lasse Laegteskov said:
Hi,

I'm writing a program for mobile phones and I have a bit of a hard time to
figure out _if_ I can read a small configuration file (text only) from the
jar-archive.

Can this be done in MIDP 1.0? And if it can, please give a small code
example...

Umm... I have not tested, for text file, but I did for picture. Use the
getResourceAsStream(String) from the Class Object
<sniplet>
// in the class that extends MIDlet
public String getFile(String file) throws Exception {
InputStream is = this.getClass().getResourceAsStream(file);
int len=0;
byte[] data = new byte[64];
StringBuffer sb = new StringBuffer();
while((len=is.read(data))!=-1) {
sb.append(new String(data,0,len));
}
is.close();
return sb.toString();
}
</sniplet>

I've done this for text files and it works.

Watch out that the resource name includes the whole package hierarchy, e.g.:
"/com/company/app/data/config.txt"

Hugh
 
L

Lasse Laegteskov

Hmm, when it runs in Suns Studio it works fine, but when I try it outside
e.g. in the emulator I get a NullPointerException in the line:
"while((len=is.read(data))!=-1) {". I guess it couldn't find the file? The
file is placed in the root ( / ) of the JAR-archive, so I load it with
/mobile.cfg. What could be wrong?


Hugh Beyer said:
Lasse Laegteskov said:
Hi,

I'm writing a program for mobile phones and I have a bit of a hard time to
figure out _if_ I can read a small configuration file (text only) from the
jar-archive.

Can this be done in MIDP 1.0? And if it can, please give a small code
example...

Umm... I have not tested, for text file, but I did for picture. Use the
getResourceAsStream(String) from the Class Object
<sniplet>
// in the class that extends MIDlet
public String getFile(String file) throws Exception {
InputStream is = this.getClass().getResourceAsStream(file);
int len=0;
byte[] data = new byte[64];
StringBuffer sb = new StringBuffer();
while((len=is.read(data))!=-1) {
sb.append(new String(data,0,len));
}
is.close();
return sb.toString();
}
</sniplet>

I've done this for text files and it works.

Watch out that the resource name includes the whole package hierarchy, e.g.:
"/com/company/app/data/config.txt"

Hugh
 
H

Hugh Beyer

Make sure the case matches. Make sure the file is actually loaded into
your JAR. Check the value of is and verify that it really is null.

Hugh


Hmm, when it runs in Suns Studio it works fine, but when I try it outside
e.g. in the emulator I get a NullPointerException in the line:
"while((len=is.read(data))!=-1) {". I guess it couldn't find the file? The
file is placed in the root ( / ) of the JAR-archive, so I load it with
/mobile.cfg. What could be wrong?


Hugh Beyer said:
"Lasse Laegteskov" <lasse(removefornospam)@danbbs.dk>
Hi,

I'm writing a program for mobile phones and I have a bit of a hard time
to figure out _if_ I can read a small configuration file (text only)
from the jar-archive.

Can this be done in MIDP 1.0? And if it can, please give a small code
example...

Umm... I have not tested, for text file, but I did for picture. Use the
getResourceAsStream(String) from the Class Object <sniplet>
// in the class that extends MIDlet
public String getFile(String file) throws Exception {
InputStream is = this.getClass().getResourceAsStream(file);
int len=0;
byte[] data = new byte[64];
StringBuffer sb = new StringBuffer();
while((len=is.read(data))!=-1) {
sb.append(new String(data,0,len)); } is.close();
return sb.toString(); } </sniplet>

I've done this for text files and it works.

Watch out that the resource name includes the whole package hierarchy,
e.g.: "/com/company/app/data/config.txt"

Hugh
 
L

Lasse Laegteskov

Thanks Hugh,

Found out by trial and error that when the JAR-archive was build, the
..cfg-file should be located in the res/ folder. This res/ is mapped as the
root / I guess? (I'm a bit new to JAR-files...)


It works! Thanks...



Hugh Beyer said:
Make sure the case matches. Make sure the file is actually loaded into
your JAR. Check the value of is and verify that it really is null.

Hugh


Hmm, when it runs in Suns Studio it works fine, but when I try it outside
e.g. in the emulator I get a NullPointerException in the line:
"while((len=is.read(data))!=-1) {". I guess it couldn't find the file? The
file is placed in the root ( / ) of the JAR-archive, so I load it with
/mobile.cfg. What could be wrong?


Hugh Beyer said:
[email protected]:

"Lasse Laegteskov" <lasse(removefornospam)@danbbs.dk>
Hi,

I'm writing a program for mobile phones and I have a bit of a hard time
to figure out _if_ I can read a small configuration file (text only)
from the jar-archive.

Can this be done in MIDP 1.0? And if it can, please give a small code
example...

Umm... I have not tested, for text file, but I did for picture. Use the
getResourceAsStream(String) from the Class Object <sniplet>
// in the class that extends MIDlet
public String getFile(String file) throws Exception {
InputStream is = this.getClass().getResourceAsStream(file);
int len=0;
byte[] data = new byte[64];
StringBuffer sb = new StringBuffer();
while((len=is.read(data))!=-1) {
sb.append(new String(data,0,len)); } is.close();
return sb.toString(); } </sniplet>

I've done this for text files and it works.

Watch out that the resource name includes the whole package hierarchy,
e.g.: "/com/company/app/data/config.txt"

Hugh
 
Joined
Jan 7, 2009
Messages
2
Reaction score
0
hi, is it possible to read and write the text file ihat is preaent out side the jar..?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top