Sagewire Logo

readLine from a Jar file

2 Message(s) by 2 Author(s) originally posted in java api


From: massmail Date:   Tuesday, June 29, 2004
I've a jar (cfg.jar) which contains a configuration file (cfg.txt)
for an application. The format of the configuration file is such that
the '#' char at the beginning of the line indicates comments to be
ignored. The uncommented lines contain key value string s for the
application. Below is the first few lines of the expanded
configuration file
#****************************************************
# Lines beginning with "#" char are comments.
SEQUENCE_SELECT_DISAPPEAR_PAUSE=500
#****************************************************
# Pause duration after sequence finishes
SEQUENCE_SELECT_REAPPEAR_PAUSE=250The following code outputs no lines of the compressed cfg.txt file.
When the System.out.println line is uncommented in main, the output
looks as follows:

false
#****************************************************
truepublic class ReadJarCFG
{
public static void main(String[]args){
try{
MyJarLineReader reader = new MyJarLineReader();
String line = reader.readLine();
while(line != null){
System.out.println(line == null);
if( line.startsWith("#") ){
//System.out.println(line);
line = reader.readLine();
System.out.println(line == null);
}
else{
System.out.println(line);
line = reader.readLine();
}
}
}catch(IOException e2){System.out.println("Exception caught");}
}
}

class MyJarLineReader
{
String CFGJAR = new String("C:\\Program
Files\\LptJarResrcs\\ReadJar\\cfg.jar");

private File lptJar = new File(cfgJar);
private JarFile jarFile = null;
private ZipEntry CFGTEXTZIP = new ZipEntry("cfg.txt");
private InputStream iStream;

public MyJarLineReader()throws IOException{
jarFile = new JarFile(lptJar, true, jarFile.OPEN_READ);
iStream = jarFile.getInputStream(cfgTextZip);
}

public String readLine() throws IOException
{
BufferedReader reader = new LineNumberReader( new
InputStreamReader(iStream) );
return reader.readLine();
}
}
I've expanded the cfg.jar file to confirm that it isn't corrupted.

Thanks in advance for any help


From: Chris Date:   Tuesday, June 29, 2004
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

wrote in message:

[snip]
class MyJarLineReader
{
String CFGJAR = new String("C:\\Program
Files\\LptJarResrcs\\ReadJar\\cfg.jar");
private File lptJar = new File(cfgJar);
private JarFile jarFile = null;
private ZipEntry CFGTEXTZIP = new ZipEntry("cfg.txt");
private InputStream iStream;
public MyJarLineReader()throws IOException{
jarFile = new JarFile(lptJar, true, jarFile.OPEN_READ);
iStream = jarFile.getInputStream(cfgTextZip);
}
public String readLine() throws IOException
{
BufferedReader reader = new LineNumberReader( new
InputStreamReader(iStream) );
return reader.readLine();
}
}
[snip]



Hi,
The problem is simple. You're creating a LineNumberReader for each
line you try to read. That's not going to work. LineNumberReader is a
type of BufferedReader. This means it buffers data from the
underlying InputStreamReader, which acquires that buffered data from
the underlying InputStream. The first call to readLine() creates a
BufferedReader. The call to readLine() tells the buffered reader to
read a line. Because it's *buffered*, it actually reads a whole load
of data. That way, next time, it can hopefully return another line
from its own buffer, rather than having to ask the InputStreamReader
for any more data. Buffering is a good thing, because it enhances
performance, but only if you use it properly. Otherwise, it can lose
data, as you see here. What your problem is, is that you are creating
a new BufferedReader (actually LineNumberReader) for each line. This
is not going to work. You want to create a *single* LineNumberReader
(probably in the constructor) and save it in a variable in place of
the InputStream. Then, for each readLine() call, simply invoke
readLine() on the saved Reader.

Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA4aw0nwjA8LryK2IRAiQLAKCSnbo3MmAObCNSaqF728bV/sb3KgCgnJ6P
/qeQZOJZBM3TWU2DaURE6+c=
=F6uH
-----END PGP SIGNATURE-----



Next Message: How can I get unbuffered InputStream from Process?


Blogs related to readLine from a Jar file

java程序员面试之150++
答:不同类型的EJB涉及的配置文件不同,都涉及到的配置文件 包括ejb-jar.xml,weblogic-ejb-jar.xml,CMP实体Bean ... readLine()); in = new BufferedReader(new InputStreamReader(socket. ... FileReader reader = new FileReader(file); Parser parser; ...

为你的应用程序添加动态Java代码
readLine(); postman.deliverMessage(msg); ... Enter a message: I wanna go to the text file. DynaCode Init class sample.PostmanImpl ... 最简单的方式,你可以仅用 一条语句编译java文件,这需要系统在类路径上包含javac编译器的tools.jar(你可以 ...

Hypersonic SQL DB - JDBC connection fails
Without Netbeans I set the Classpath (ie " set CLASSPATH=f:\hsqldb.jar org.hsqldb. ... NoClassDefFoundError : joy My Source code for "joy.java" is given below ... readLine(); } catch(IOException ex) { System.out.println("Exception ...

ES_NODE_ROOT/config directory not created after install
ESLogControlResponder from C:\Program Files\IBM\es\lib\ccl.jar May 14, 2006 12:53:07 PM com.ibm.es.ccl.server.impl.ESAdminProperties StartResponders INFO: ... readLine(DataInputStream.java:589) at com.ibm.es.ccl.server.responders.sys. ...

Using Java compiler in your Web Start application
The standard technique for compiling Java source files in regular ... A manifest file is, in fact, the most prominent component that makes jar file ... readLine(); if (str == null) { break; } this._logger.info("/javac/ " + str); ...

JAVA相关基础知识
<%@include file=”filename”%> <%@taglib prefix=”c”uri=”http://……”%> 60、什么情况 下调用doGet()和doPost()? ... xmlCMP实体Bean一般还需要weblogic-cmp-rdbms-jar.xml ... readLine()); in = new BufferedReader(new InputStreamReader(socket. ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional