Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

BufferedReader reads same line over and over in a loop?

807607Oct 26 2006 — edited Oct 26 2006
Hi. I'm trying to build a method that lets me read all the text from a file, and then returns it as a single string.
I've found myself having to do this with BufferedReader's readLine() method.
Unfortunately, the only way I could think of to get it to read the entire file without some sort of pre-determined linecount was to do it in the form of a Do-While loop, in which the BufferedReader reads a line, assigns it to a String created outside the loop, and then runs again and again until an Exception is thrown indicating that it has reached the end of the file and can't read any more. When the exception is thrown, the method ends the loop and returns all the read text.

Here's the code:
    /**
     * Reads an entire file then returns a String containing the text.
     * 
     * @param fn The filepath of the file to create the stream to.
     * @return A String containing the text in the file. Blank string if an error occurs or no text is found.
     */
    public String readFileSS(String fn) {
        String value = "";
        try {
            BufferedReader br = setupFileIn(fn);
            int finished = 0;
            do {
                try {
                    value = value + br.readLine() + "\n";
                }
                catch (Exception e) {
                    finished = 1;
                }
            } while (finished == 0);
        }
        catch (Exception e) {
            if (debug) err("readFileSS", e);
        }
        return value;
    }
And here's what happens:
The loop runs endlessly! The BufferedReader seems to read the same line over and over, never encountering the end of the file and thus never throwing an Exception and thus never ending the loop.

What am I doing wrong here? I feel so stupid. ;-)
Or...
Is there any easier way to do this? hee hee. ;-)

EDIT: Here's the method used within the one above, setupFileIn(string):
    /**
     * Creates an "in" stream from a file.
     * 
     * @param fn The filepath of the file to create the stream to.
     * @return Returns a BufferedReader instance with access to the specified file. Null if an error occurs.
     */
    public BufferedReader setupFileIn(String fn) {
        BufferedReader br = null;
        try {
            FileInputStream fis = new FileInputStream(fn);
            try {
                br = new BufferedReader(new InputStreamReader(fis));
            }
            catch (Exception e) {
                if (debug) err("setupFileIn", e);
            }
        }
        catch (Exception e) {
            if (debug) err("setupFileIn", e);
        }
        return br;
    }
Both these methods are part of my package aplty named "LF".

Message was edited by:
LukeFoss
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 23 2006
Added on Oct 26 2006
3 comments
1,596 views