【问题标题】:Can't understand the output sequence, using Scanner & FileReader to read a text file & print无法理解输出顺序,使用 Scanner 和 FileReader 读取文本文件并打印
【发布时间】:2020-06-20 15:29:19
【问题描述】:

我正在尝试阅读一个简单的书单文本文件,其中书名和作者用“-”分隔。使用扫描仪、文件阅读器和跳过将书名与作者分开并打印。我无法理解输出的程序流程:

 import java.io.*;
    import java.util.*;

public class FileReaderDemo {

    public static void main(String[] args)  {

        File file = new File("Filepath....>BookList.txt");
        Scanner scan = null;

        try {
                scan = new Scanner(new FileReader(file));
                scan.useDelimiter("-");

                while(scan.hasNext())
                {
                    System.out.println("Book -> "+scan.next());
                    scan.skip(scan.delimiter());
                    System.out.println("Author -> "+scan.next());

                }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        scan.close();       


}
}

文本文件包含以下内容:

Harry Potter & Order of Phoenix - J K Rowling
Autobiography of a Yogi - Parmahansa Yoganand
Alice in Wonderland - Lewis Carroll

输出如下:

Book -> Harry Potter & Order of Phoenix 
Author ->  J K Rowling
Autobiography of a Yogi 
Book ->  Parmahansa Yoganand
Alice in Wonderland 
Author ->  Lewis Carroll

我无法理解的是,第一本书打印得很好,第二本书的名称如何打印没有“Book->”或前面的任何 println 语句,然后再次打印第三本书,最后是作者最后一部分?

【问题讨论】:

    标签: java java.util.scanner


    【解决方案1】:

    当您将分隔符设置为- 时,仅 - 分隔标记,换行符不会,这就是为什么第二个标记是文本"J K Rowling\nAutobiography of a Yogi"。

    如果您想在- 和换行符处进行分隔,请使用正则表达式"-|\\R"。

    您还想删除- 周围的空格,因此将其作为分隔符的一部分,或在返回的字符串上调用trim()。

    演示

    String input = "Harry Potter & Order of Phoenix - J K Rowling\r\n" + 
                   "Autobiography of a Yogi - Parmahansa Yoganand\r\n" + 
                   "Alice in Wonderland - Lewis Carroll\r\n";
    try (Scanner scan = new Scanner(input)) {
        scan.useDelimiter("\\s*-\\s*|\\s*\\R");
        while (scan.hasNext()) {
            System.out.println("Book -> " + scan.next());
            System.out.println("Author -> " + scan.next());
        }
    }
    

    输出

    Book -> Harry Potter & Order of Phoenix
    Author -> J K Rowling
    Book -> Autobiography of a Yogi
    Author -> Parmahansa Yoganand
    Book -> Alice in Wonderland
    Author -> Lewis Carroll
    

    【讨论】:

      【解决方案2】:

      使用scan.useDelimiter("-"),您可以在不考虑换行符的情况下对源文件的字符串进行标记。

      我会改变方法并使用.nextLine() 方法代替.next(),然后使用String 的.split("-") 方法拆分行。

      或者,您可以将扫描仪分隔符设置为

      scan.useDelimiter(System.getProperty("line.separator"))
      

      【讨论】:

      • 您能详细说明一下吗?我试过这个: while(scan.hasNextLine()) { System.out.println("Book -> "+scan.nextLine()); scan.nextLine().split("-"); System.out.println("作者 -> "+scan.nextLine()); } 不起作用
      • 这里有:File file = new File("<path>"); Scanner scan = null; try { scan = new Scanner(new FileReader(file)); while (scan.hasNextLine()) { String line = scan.nextLine(); String[] elems = line.split("-"); System.out.println("Book -> " + elems[0].trim()); System.out.println("Author -> " + elems[1].trim()); } } catch (FileNotFoundException e) { e.printStackTrace(); }
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 2012-09-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      相关资源
      最近更新 更多