0

谁能在这里指出我正确的方向。我有一个方法应该读取文件并显示该文件中的数据。我只能让它显示一行。我知道这很简单,但我的大脑已经糊涂了,我只是不断地挖一个更大的洞。

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

}
4

2 回答 2

2
String line = null;
while ((line = bReader.readLine())!=null)
于 2013-10-16T00:48:02.133 回答
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>线条等......

于 2013-10-16T00:49:22.410 回答