Reputation: 153
Is it possible to read a specific value from CSV specifed the row number and column number .
For example, my CSV data looks like below,
CREATE,BulkDevice4981,SENSOR,localhost
CREATE,BulkDevice4081,SENSOR,localhost
I want to read value from row "1" and column "2" without pharsing through the entire CSV.
I need help this regard. Please help me
Upvotes: 0
Views: 166
Reputation: 6381
There is no way you can read nth row without looping through previous rows, but it doesn't imply reading whole file.
So, to achieve your goal you need to read nth line of file:
try (Stream<String> lines = Files.lines(Paths.get("path/to/your/file"))) {
String line = lines.skip(LINE_NUMBER).findFirst().get();
}
Then, you can simply split the line by comma and read value from columns:
String line = ...
String[] columns = line.split(",")
// columns[2] should contain your value
This is however rather primitive solution and it would be better to use dedicated CSV reader like opencsv
Upvotes: 1