Jim
Jim

Reputation: 51

Output only giving me one line

Can anyone point me in the right direction here. I have a method that is supposed to read a file and display the data in that file. I can only get it to display one line. I know it is something simple I am over looking, but my brain is mush and I just keep digging a bigger hole.

public static String readFile(String file) {
    String data = "";
    if (!new java.io.File(file).exists()) {
        return data;
    }
    File f = new File(file);
    FileInputStream fStream = null;
    BufferedInputStream bStream = null;
    BufferedReader bReader = null;
    StringBuffer buff = new StringBuffer();

    try {
        fStream = new FileInputStream(f);
        bStream = new BufferedInputStream(fStream);
        bReader = new BufferedReader(new InputStreamReader(bStream));
        String line = "";

        while (bStream.available() != 0) {
            line = bReader.readLine();

            if (line.length() > 0) {
                if (line.contains("<br/>")) {
                    line = line.replaceAll("<br/>", " ");
                    String tempLine = "";
                    while ((tempLine.trim().length() < 1)
                            && bStream.available() != 0) {
                        tempLine = bReader.readLine();
                    }
                    line = line + tempLine;
                }
                buff.append(line + "\n");

            }
        }

        fStream.close();
        bStream.close();
        bReader.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return buff.toString();

}

Upvotes: 0

Views: 96

Answers (2)

Kong
Kong

Reputation: 9586

How about doing this with Guava:

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/Files.html

List<String> lines = Files.readLines("myFile.txt", Charset.forName("UTF-8"));
System.out.println(lines);

You'd still have to do a little bit of work to concatenate the <br> lines etc...

Upvotes: 1

Alex
Alex

Reputation: 11579

String line = null;
while ((line = bReader.readLine())!=null)

Upvotes: 2

Related Questions