【问题标题】:Using delimiter when reading a file读取文件时使用分隔符
【发布时间】:2012-12-23 23:00:26
【问题描述】:

我几乎没有使用分隔符的经验,我需要读取一个文本文件,该文件存储多个对象,这些对象的数据存储在单行中,由逗号 (",") 分隔。然后使用单独的字符串创建一个新对象,该对象被添加到数组列表中。

Amadeus,Drama,160 Mins.,1984,14.83
As Good As It Gets,Drama,139 Mins.,1998,11.3
Batman,Action,126 Mins.,1989,10.15
Billy Elliot,Drama,111 Mins.,2001,10.23
Blade Runner,Science Fiction,117 Mins.,1982,11.98
Shadowlands,Drama,133 Mins.,1993,9.89
Shrek,Animation,93 Mins,2001,15.99
Snatch,Action,103 Mins,2001,20.67
The Lord of the Rings,Fantasy,178 Mins,2001,25.87

我正在使用扫描仪读取文件,但是我得到一个找不到行的错误,整个文件被存储到一个字符串中:

Scanner read = new Scanner (new File("datafile.txt"));
read.useDelimiter(",");
String title, category, runningTime, year, price;

while (read.hasNext())
{
   title = read.nextLine();
   category = read.nextLine();
   runningTime = read.nextLine();
   year = read.nextLine();
   price = read.nextLine();
   System.out.println(title + " " + category + " " + runningTime + " " +
                      year + " " + price + "\n"); // just for debugging
}
read.close();

【问题讨论】:

  • 使用read.next() 而不是nextLine()

标签: java delimiter


【解决方案1】:

一个问题是:

while(read.hasNext())
   {
       title = read.nextLine();
       category = read.nextLine();
       runningTime = read.nextLine();

hasNext()

如果此扫描器在其输入中有另一个令牌,则返回 true。不是整行。你需要使用hasNextLine()

你正在执行 nextLine() 三次。我认为您需要做的是,阅读该行并拆分该行。

【讨论】:

    【解决方案2】:

    您还可以使用 String.split() 函数将字符串转换为字符串数组,然后遍历每个字符串以获得您的值。

    How to convert comma-separated String to ArrayList?查看更多详情。

    【讨论】:

      【解决方案3】:

      您应该在使用 nextLine(); 的地方使用 next();

      看看教程:http://docs.oracle.com/javase/tutorial/essential/io/scanning.html

      注意以下几行:

      try {
         s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
      
         while (s.hasNext()) {
         System.out.println(s.next());
      }
      

      【讨论】:

        【解决方案4】:

        使用 read.next() 代替 read.nextLine()

           title = read.next();
           category = read.next();
           runningTime = read.next();
           year = read.next();
           price = read.next();
        

        【讨论】:

          【解决方案5】:

          我想你想调用.next(),它返回一个字符串而不是.nextLine()。您的 .nextLine() 呼叫正在超出当前行。

          Scanner read = new Scanner (new File("datafile.txt"));
             read.useDelimiter(",");
             String title, category, runningTime, year, price;
          
             while(read.hasNext())
             {
                 title = read.next();
                 category = read.next();
                 runningTime = read.next();
                 year = read.next();
                 price = read.next();
               System.out.println(title + " " + category + " " + runningTime + " " + year + " " + price + "\n"); //just for debugging
             }
             read.close();
          

          【讨论】:

            【解决方案6】:

            以上所有答案都是正确的,实际上是相同的。但是,每个人都应该记住一点,Scanner 的缓冲区大小只有 1024。这意味着如果分隔文本的长度更长,解析将停止。

            因此,对给定的解决方案进行一点改进,使用BufferedReader 而不是直接将文件传递给Scanner。示例:

                BufferedReader in = new BufferedReader(new FileReader("datafile.txt"), 16*1024);
                Scanner read = new Scanner(in);
                read.useDelimiter(",");
                String title, category, runningTime, year, price;
            
                while(read.hasNext())
                {
                    title = read.next();
                    category = read.next();
                    runningTime = read.next();
                    year = read.next();
                    price = read.next();
                    System.out.println(title + " " + category + " " + runningTime + " " + year + " " + price + "\n"); //just for debugging
                }
                read.close();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2019-12-07
              • 1970-01-01
              • 1970-01-01
              • 2020-11-21
              • 2013-12-11
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多