Reading a text file in archive in jar

Hi,

I am unable to read a text file in my jar archive.

Example code:

try

{

InputConnection con =

(InputConnection) Connector.open("file://text/info.txt", Connector.READ);

InputStream in = con.openInputStream();

StringBuffer buf = new StringBuffer();

int ch;

while((ch = in.read()) != -1 )

{

buf.append((char) ch);

}

in.close();

form.append(buf.toString());

} catch (Exception e) { e.printStackTrace(); }

Any ideas how I can do that?

Thanks.

[572 byte] By [chenshuhan] at [2007-9-26 1:32:33]
# 1

You have to use java.lang.Class`s getResourceAsStream method. Your code:

try

{

StringBuffer buf;

Class mc = buf.getClass();

InputStream in = mc.getResourceAsStream("/text/info.txt");

buf = new StringBuffer();

int ch;

while((ch = in.read()) != -1 )

{

buf.append((char) ch);

}

in.close();

form.append(buf.toString());

} catch (Exception e) { e.printStackTrace(); }

29101989 at 2007-6-29 1:33:44 > top of Java-index,Java Mobility Forums,Java ME Technologies...