跳线时读取txt文件中的单词与扫描仪

时间:2015-11-01 21:33:52

标签: java file lines

我在扫描仪中读取文字,但我需要知道扫描仪是否更改为以下行。这是一个进度条(计数线)。 你能帮助我吗?这是我的代码:

Pattern pattern = Pattern.compile("[^\\p{Alpha}]+");
try (Scanner sc = new Scanner(file)) {
               while (sc.hasNext()) {
               sc.useDelimiter(pattern);
               long totalLines = countLines(f);//Method that count Lines
               System.out.println("Reading " + totalLines + "Lines...");
               word = sc.next();//here i need to know if the scanner jumps to next Line or not. 

1 个答案:

答案 0 :(得分:0)

使用Scanner类中提供的hasNextLine()nextLine()方法。

遍历行。对于每一行,使用空格拆分并计算其中的单词数。