Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read the second column in a large file

Tags:

java

file

I have a huge file with millions of columns, splited by space, but it only has a limited number of rows:

examples.txt:

1 2 3 4 5 ........
3 1 2 3 5 .........
l 6 3 2 2 ........

Now, I just want to read in the second column:

2
1
6

How do I do that in java with high performance.

Thanks

Update: the file is usually 1.4G containing hundreds of rows.

like image 281
Frank Avatar asked Jun 19 '12 00:06

Frank


People also ask

How do you select one column in a text file?

ALT + Left Mouse Click puts you in Column Mode Select. It's quite an useful shortcut that may help you. This is by far the simplest solution.

How do I extract a column from a data file in Python?

Make a list of columns that have to be extracted. Use read_csv() method to extract the csv file into data frame. Print the exracted data. Plot the data frame using plot() method.


1 Answers

If your file is not statically structured, your only option is the naive one: read through the file byte sequence by byte sequence looking for newlines and grab the second column after each one. Use FileReader.

If your file were statically structured, you could calculate where in the file the second column would be for a given line and seek() to it directly.

like image 57
cheeken Avatar answered Sep 19 '22 10:09

cheeken