deltanovember
deltanovember

Reputation: 44061

In Scala, when reading from a file how would I skip the first line?

The file is very large so I cannot store in memory. I iterate line by line as follows

for (line <- Source.fromFile(file).getLines) {
}

How can I specify that the first line should be skipped?

Upvotes: 15

Views: 9992

Answers (1)

Ben James
Ben James

Reputation: 125217

How about:

for (line <- Source.fromFile(file).getLines.drop(1)) {
  // ...
}

drop will simply advance the iterator (returned by getLines) past the specified number of elements.

Upvotes: 45

Related Questions