【问题标题】:Java - read text file starting from second lineJava - 从第二行开始读取文本文件
【发布时间】:2017-03-01 06:13:12
【问题描述】:

我正在尝试在 java 中读取一个 txt 文件。但是,我只想从第二行开始阅读,因为第一行只是一个标签。这是例子

文本文件:

Name,Type,Price
Apple,Fruit,3
Orange,Fruit,2
Lettuce,Veggie,1

我该怎么做?我有这段代码,你可以从第一行读取。

代码:

//read the file, line by line from txt
File file = new File("train/traindata.txt");
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;

line = br.readLine();

while(line != null)
{
    lines = line.split(",");

    //Do something for line here
    //Store the data read into a variable

    line = br.readLine();         
}

fr.close();

请帮助我,提前谢谢。

【问题讨论】:

    标签: java csv io


    【解决方案1】:

    如果您有兴趣使用第三方库,这里是一个使用 Apache Commons CSV 的示例(它将跳过标题,但保留其映射以从记录中检索字段)。

    根据文件的编码修改字符集。

       CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"),CSVFormat.RFC4180.withFirstRecordAsHeader().withSkipHeaderRecord());
    
       List<CSVRecord> records = parser.getRecords();
    
       for (CSVRecord record : records) {
    
           System.out.println(record.get("Name"));
           System.out.println(record.get("Type"));
           System.out.println(record.get("Price"));
       }
    

    【讨论】:

      【解决方案2】:

      我认为您正在将 txt 文件转换为 CSV Parser

      所以我建议你..

      br.readLine(); // Header of CSV
      line = br.readLine();
      while(line != null)
      {
       // Your Logic
      } 
      

      【讨论】:

        【解决方案3】:

        我提出了一个不同的解决方案:忽略线条而不看它们......当然有效;但是这种方法在更改文件内容时不是很可靠!

        如果您将文件更改为具有会发生什么

        header
        
        data
        

        data
        data
        

        所以,我的建议是这样的 --- 保留您当前的代码,但确保您只选择具有 valid 数据的行;例如通过修改你的循环体:

        lines = line.split(",");
        if (lines.length == 3 && isNumber(lines[2])) ...
        

        其中 isNumber() 是一个小辅助函数,用于检查传入的字符串是否正确,一个数字。

        换句话说:故意将有关文件布局的硬代码知识隐式地跳过行到您的“解析器”中。这对于简单的练习来说可能没问题,但在现实世界中,这样的事情在未来的某个时候打破。然后乐趣就开始了。因为 nobody 会记住,解析代码是为了丢掉文件的第一行而写的。

        如图所示,您可以轻松避免此类问题!

        【讨论】:

          【解决方案4】:

          只需在while条件下执行以下操作:

          line = br.readLine();
          
          while((line=br.readLine()) != null)
          {
              lines = line.split(",");
          
              //Do something for line here
              //Store the data read into a variable
          
              line = br.readLine();         
          }
          
          fr.close();
          

          【讨论】:

            【解决方案5】:

            只需阅读并跳过第一行

            //read the file, line by line from txt
            File file = new File("train/traindata.txt");
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line;
            
            line = br.readLine();
            boolean first = true;
            while(line != null)
            {
                if (first) {
                  first = false;
                } else {
                  lines = line.split(",");
            
                  //Do something for line here
                  //Store the data read into a variable
            
                  line = br.readLine();         
                }
            }
            
            fr.close();
            

            【讨论】:

              【解决方案6】:

              只需添加一个额外的BufferedReader#readLine 呼叫...

              br.readLine(); // consume first line and ignore
              line = br.readLine();
              while(line != null) ...
              

              【讨论】:

              • 这个答案证明了“不要让它变得比它必须的更难”:-)
              • @Gikkman 但它也是一个很好的例子,不是很健壮的代码是如何出现的。
              • @GhostCat 你有一个很好的观点。根据项目所需的稳健性和寿命,简单的解决方案并不总是正确的
              • 感谢您的解决方案,这是文本文件的要求哈哈
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-01-14
              • 1970-01-01
              • 2013-08-27
              相关资源
              最近更新 更多