AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

readLine from a Jar file

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.api
View previous topic :: View next topic  
Author Message
Guy Sussman
Guest





PostPosted: Tue Jun 29, 2004 4:39 pm    Post subject: readLine from a Jar file Reply with quote



I have 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 strings 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=250


The 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
#****************************************************
true


public 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 have expanded the cfg.jar file to confirm that it is not corrupted.

Thanks in advance for any help
Back to top
Chris
Guest





PostPosted: Tue Jun 29, 2004 5:51 pm    Post subject: Re: readLine from a Jar file Reply with quote



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Guy Sussman wrote:

[snip]
Quote:
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
isn't 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-----

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.api All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.