如何使用BufferedReader

时间:2016-12-05 18:04:40

标签: java bufferedreader

我刚刚被这个BufferedReader困住了,我无法让它读取整个txt文件..它只读取第一行!

FileReader fr = new FileReader("/Users/esson/Desktop/sonnets/sonnet3.txt");
BufferedReader br = new BufferedReader(fr);
String input = br.readLine();
List<String> output= (List) Arrays.asList(input.split(" "));

for(String word: output) {
    int times = Collections.frequency(output, word);
    System.out.println("" + word+ " -- "+times);

,输出为:

When -- 1
most -- 1
I -- 1
wink -- 1
then -- 1
do -- 1
mine -- 1
eyes -- 1
best -- 1
see, -- 1

2 个答案:

答案 0 :(得分:1)

您需要将BufferedReader.readLine()放入循环中。例如:

while((text = BufferedReader.readLine()) != null)

另外,我认为您应该将问题标记为Java而不是Javascript

答案 1 :(得分:0)

int lineNum;

for(String word: output) {

     lineNum++;

     int times = Collections.frequency(output, word);
     System.out.println("" + word+ " -- "+times);
 }

 System.out.println("Line Number is " + lineNum);