【问题标题】:BufferedReader to skip first lineBufferedReader 跳过第一行
【发布时间】:2014-06-07 19:06:48
【问题描述】:

我正在使用以下bufferedreader 来读取文件的行,

BufferedReader reader = new BufferedReader(new FileReader(somepath));
while ((line1 = reader.readLine()) != null) 
{
    //some code
}

现在,我想跳过读取文件的第一行,并且不想使用计数器行 int lineno 来记录行数。

如何做到这一点?

【问题讨论】:

  • 为什么不阅读Line并忽略它?
  • 在循环前调用reader.readLine()
  • @AndrewLogvinov 谢谢!我已经使用了您的方法,并且似乎是最有效的方法,请将其发布为答案。

标签: java bufferedreader


【解决方案1】:

你可以试试这个

 BufferedReader reader = new BufferedReader(new FileReader(somepath));
 reader.readLine(); // this will read the first line
 String line1=null;
 while ((line1 = reader.readLine()) != null){ //loop will run from 2nd line
        //some code
 }

【讨论】:

  • 简单。我怎么没想到呢? :D
【解决方案2】:

您可以使用 Stream skip() 函数,如下所示:

BufferedReader reader = new BufferedReader(new FileReader(somepath));   
Stream<String> lines = reader.lines().skip(1);

lines.forEachOrdered(line -> {

...
});

【讨论】:

  • 虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
  • 这个很完美。
【解决方案3】:
File file = new File("path to file");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
int count = 0;
while((line = br.readLine()) != null) { // read through file line by line
    if(count != 0) { // count == 0 means the first line
        System.out.println("That's not the first line");
    }
    count++; // count increments as you read lines
}
br.close(); // do not forget to close the resources

【讨论】:

  • 添加有关代码工作方式的 cmets 将帮助用户更好地理解您的代码。
  • 希望我的代码现在使用 cmets 更加清晰。感谢您的警告;)
【解决方案4】:

改用行号阅读器。

LineNumberReader reader = new LineNumberReader(new InputStreamReader(file.getInputStream()));
            String line1;
            while ((line1 = reader.readLine()) != null) 
            {
                if(reader.getLineNumber()==1){
                    continue;
                }
                System.out.println(line1);
            }

【讨论】:

  • 这很可能是解决这个问题的最低效和最复杂的方法。
  • @BrianRoach 为什么你认为 linenumberreader 过于复杂?
  • 因为 A)没有必要,B)你在每次读取时都进行了不必要的比较。
  • A) 它使您可以控制跳过任何您希望的行。不限于第一行。 B)我在问题中没有看到任何与性能问题相关的评论。我希望用户一定会自定义解决方案,而不是复制粘贴。实际上,这里甚至不需要 InputstreamReader,fileReader 就可以了。我们是不是太固执己见了?
  • 我的个人意见是,当你只需要一架双翼飞机时,为什么要建造航天飞机。您想跳过文件中的第一行。你有一个BufferedReader。在循环之前调用readLine()。完成。
【解决方案5】:

您可以创建一个包含起始行值的计数器:

private final static START_LINE = 1;

BufferedReader reader = new BufferedReader(new FileReader(somepath));
int counter=START_LINE;

while ((line1 = reader.readLine()) != null) {
  if(counter>START_LINE){
     //your code here
  }
  counter++;
}

【讨论】:

    【解决方案6】:

    你可以这样做:

    BufferedReader buf = new BufferedReader(new FileReader(fileName));
                String line = null;
                String[] wordsArray;
                boolean skipFirstLine = true;
    
    
    while(true){
                    line = buf.readLine();
                    if ( skipFirstLine){ // skip data header
                        skipFirstLine = false; continue;
                    }
                    if(line == null){  
                        break; 
                    }else{
                        wordsArray = line.split("\t");
    }
    buf.close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-27
      • 2012-05-02
      • 2011-11-04
      • 1970-01-01
      • 2013-07-04
      • 2013-08-17
      • 2013-03-08
      • 1970-01-01
      相关资源
      最近更新 更多