The Artima Developer Community
Sponsored Link

Java Answers Forum
Need some help with BufferedReader

3 replies on 1 page. Most recent reply: Jul 24, 2002 1:30 AM by Joey

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Need some help with BufferedReader Posted: Jul 11, 2002 1:51 PM
Reply to this message Reply
Advertisement
I have a class that reads in the contents of a file and returns the contents as a string array. My methods are:
initIn //initializes the BufferedReader object in
closeIn // calls in.close()
getFileContents //gets the file's contents
getFileSize //gets the size of the file (how many lines there are in the file)

initIn and getFileContents look like this:
<java>
private void initIn()
{
in = new BufferedReader(new FileReader(fileName));
}

public String[] getFileContents()
{
initIn();

String[] t = new String[getFileSize()];
closeIn();

initIn();
// Gets the contents. I won't post the code.
closeIn();

return t;
}
</java>

After I figure out how many lines the file contains (by calling in.readLine() until it returns null), I create the string array with that many elements. Then I close the stream, reopen it (by re-initalizing in) and insert each line of the file into each successive element. Finally I close the stream again.

My problem is this:
When I call this method the first time, it works. But when I call it a second time, the getFileSize method returns 0. Apparently, the very first time I call in.readLine(), I get null. But as far as I know, the stream should have stuff in it since I re-opened the stream. Does anyone know what might be wrong? Thanks for the help.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Need some help with BufferedReader Posted: Jul 11, 2002 2:48 PM
Reply to this message Reply
It looks like you are opening the file twice but only closing once (after the second open).

It is probably not a good idea to have this separate initIn() method; it makes the code harder to read. Since creating the BufferedReader is only one line of code, it would be easier to follow the code if it were opened (and later closed) in the same context where it is used.

Also, maybe you can show the source of the other methods.

Finally, you might consider looking into Collections. It would be a lot more sensible to read the lines into a List of lines, which dynamically grows as you successively add lines to it. At the end of the file-reading process, you can then ask the list how many lines there are. If you really need an array, you can easily convert the list to an array with toArray() at that point and discard the list, if you wish. If you do all this, your code will probably be more than 50% smaller than it is now and a lot easier to understand.

Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Re: Need some help with BufferedReader Posted: Jul 11, 2002 4:32 PM
Reply to this message Reply
> It looks like you are opening the file twice but only
> closing once (after the second open).

I'm also closing the file after getting the size of the file. So, I open the file, get the size, close the file, open it again, get the contents, and finally close it. Anyway, thanks for the help. I'll look into using a Collection.

Joey

Posts: 12
Nickname: joey
Registered: Jul, 2002

Re: Need some help with BufferedReader Posted: Jul 24, 2002 1:30 AM
Reply to this message Reply
I think you best use a list, it's more efficient then opening a file twice, update, and you have some kind of help if make it visible.
Greetz Joey

Flat View: This topic has 3 replies on 1 page
Topic: tree menu--IMPORTANT Previous Topic   Next Topic Topic: popup menu on moving image

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use