【问题标题】:Resetting a .nextLine() Scanner重置 .nextLine() 扫描仪
【发布时间】:2012-12-21 13:58:03
【问题描述】:

我是 Java 的业余爱好者,所以如果我的问题看起来很愚蠢,请原谅我的问题 :-P 我有以下代码,旨在计算文件中的行数:

while (scanNumOfLines.hasNextLine())    
    {
    NumOfLines ++;
    scanNumOfLines.nextLine();
    }
    System.out.println("NumOfLines = "+NumOfLines);

所以它算不错,但我想将扫描仪重新用于其他目的,但 nextLine 已移动到文件的最后一行,我想将其重置回第一行。

(相反,为了其他目的,我不得不使用另一个扫描仪,在我看来,这似乎不如应有的优雅。)

我确定一定有一种扫描器方法可以将计数器重置为零?

谢谢

CJ

【问题讨论】:

    标签: java


    【解决方案1】:

    这是不可能的。

    不包括它的原因是它支持的输入类型范围很广。一个例子是流。这些在传递后不存储结果,因此它们不支持重置。

    所以优雅的方法是创建一个新的Scanner。如果你给它很多自定义设置,创建一个工厂方法。

    【讨论】:

      【解决方案2】:

      您必须重新声明Scanner。当您调用nextLine() 时,该行将从缓冲区中删除并有效地从Scanner 中丢弃。

      所以,本质上,有一种方法可以做到这一点:它是构造函数。

      Scanner scanNumOfLines = new Scanner(myFile);
      

      Scanner 对象中没有“计数器”。相反,它更像是一条传送带。皮带不知道也不关心上面有什么。它只是不停地向你吐东西,而上面还有东西。一旦你拿走它们,它们就会永远消失。

      【讨论】:

        【解决方案3】:

        我意识到这个问题已经得到解答,但如果我正在查找这些东西,我相信其他人也是如此,所以我想我会按照我解决这个问题的方式做出贡献: 由于我的问题需要我根据用户输入多次读取文件,因此我的测试类有一个 ArrayList 扫描仪读取文件,然后在不再需要时自行删除。我设置它的方式是,ArrayList 中的扫描仪永远不会超过 1 个,但 ArrayList 在添加和删除对象时很有用。

        public class TxtReader {
             private boolean PROGRAM_CONTINUES = true;
             private final int indexCount = 0;
             File newFile = new File("Reader Example.txt");
             ArrayList<Scanner> scanList = new ArrayList<Scanner>();
        
             public TxtReader(Scanner UserInput) throws FileNotFoundException {
                 while(PROGRAM_CONTINUES) {
                      if (UserInput.next().equalsIgnoreCase("end")) { 
                  // some arbitrary way of concluding the while loop
                          PROGRAM_CONTINUES = false;
                          break;
                      }
                      scanList.add(new Scanner(newFile));
                  // DO STUFF*********************************************
                      while(scanList.get(indexCount).hasNext()) {
                         System.out.println(scanList.get(indexCount).nextLine());
                     }
                  //******************************************************
                      scanList.get(indexCount).close(); 
                  //always close a scanner after you're done using it
                      scanList.remove(indexCount); // removes the now-unnecessary scanner 
                      UserInput = new Scanner(System.in);
                }
            }
            public static void main(String[] args) throws FileNotFoundException {
                new TxtReader(new Scanner(System.in));
            }
        }
        

        【讨论】:

          【解决方案4】:
          File file = new File("StoreData.txt");
          
          Scanner reader = new Scanner(new FileInputStream(file));
          while (reader.hasNext()) {
                  k++;
                  reader.nextLine();
          }
          reader.close();
          reader=null;    
          //reset scanner         
          reader=new Scanner(new FileInputStream(file));
          while (reader.hasNext()) {
              System.out.println(reader.nextLine());              
          }
          

          【讨论】:

            【解决方案5】:

            您可以使用RandomAccessFile 并使用seek() 方法返回第一行。

            【讨论】:

            • 但不是Scanner。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-01-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-04-21
            • 1970-01-01
            相关资源
            最近更新 更多