Подтвердить что ты не робот

BufferedReader пропустить первую строку

Я использую следующий bufferedreader для чтения строк файла,

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

Теперь я хочу пропустить чтение первой строки файла, и я не хочу использовать счетную строку int lineno, чтобы сохранить количество строк.

Как это сделать?

4b9b3361

Ответ 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
 }

Ответ 2

Вместо этого используйте прошивку.

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

Ответ 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 not the first line");
    }
    count++; // count increments as you read lines
}
br.close(); // do not forget to close the resources

Ответ 4

Вы можете создать счетчик, который содержит значение стартовой строки:

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

Ответ 5

Вы можете сделать это следующим образом:

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();