BufferedReader跳过第一行

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

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

现在,我想跳过阅读文件的第一行,我不想使用计数行int lineno来保持行的计数。

这个怎么做?

你可以试试这个

  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 } 

请改用linenumberreader。

 LineNumberReader reader = new LineNumberReader(new InputStreamReader(file.getInputStream())); String line1; while ((line1 = reader.readLine()) != null) { if(reader.getLineNumber()==1){ continue; } System.out.println(line1); } 
 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 

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

 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++; }