输出只给我一行

时间:2013-10-16 00:44:11

标签: java

任何人都可以在这里指出我正确的方向。我有一个方法应该读取文件并显示该文件中的数据。我只能让它显示一行。我知道这看起来很简单,但我的大脑很难看,我只是继续挖一个更大的洞。

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();

}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

用番石榴做这件事怎么样:

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);

你仍然需要做一些工作来连接<br>行等......