8

Can someone help me to read a selective column of data in a text file into a list..

e.g.: if the text file data is as follows

-------------
id name age
01 ron   21
02 harry 12
03 tom   23
04 jerry 25
-------------

from the above data if I need to gather the column "name" using list in java and print it..

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Venkat Pathy
  • 151
  • 2
  • 3
  • 8

3 Answers3

5

java.util.Scanner could be used to read the file, discarding the unwanted columns. Either print the wanted column values as the file is processed or add() them to a java.util.ArrayList and print them once processing is complete.

A small example with limited error checking:

Scanner s = new Scanner(new File("input.txt"));
List<String> names = new ArrayList<String>();

// Skip column headings.

// Read each line, ensuring correct format.
while (s.hasNext())
{
    s.nextInt();         // read and skip 'id'
    names.add(s.next()); // read and store 'name'
    s.nextInt();         // read and skip 'age'
}

for (String name: names)
{
    System.out.println(name);
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • thanks,, short and looks simple,, but how can we use this for a huge crowd of data,, i.e. too many columns,, nd i need 2 or more of them? also saw codes wth split("\t") used,, wat is its purpose here in this kinda codeing? – Venkat Pathy Jan 31 '12 at 18:17
  • Amend the `while` loop depending on the number of columns. Each iteration of the `while` loop represents the processing of a single line. The `split()` would be used on `String` instances when a whole line is read from the file, which is unrequired here as the `Scanner` does the splitting. – hmjd Jan 31 '12 at 18:25
3

Use a file reader and read it line by line, break on the spaces and add any column you want to the List. Use a BufferedReader to grab the lines by something like this:

BufferedReader br = new BufferedReader(new FileReader("C:\\readFile.txt"));

Then you can do to grab a line:

String line = br.readLine();

Finally you can split the string into an array by column by doing this:

String[] columns = line.split(" ");

Then you can access the columns and add them into the list depending on if you want column 0, 1, or 2.

Michael Boselowitz
  • 3,012
  • 1
  • 19
  • 22
  • Thanks,i gave this a try, but i need to have only the data which falls under the category "name", and i dont have any idea on proceeding beyond that – Venkat Pathy Jan 31 '12 at 18:33
  • If you need only what falls under name do `List.add(columns1[1])`, all this needs to be rapped in a while loop. br.readLine() will return null if it has reached the end of the file, so you can do the following: `while((line = br.readLine()) != null)`. That will continue to read every line of the file and then just add the entry in whichever column you wish to your list, like I showed you above. – Michael Boselowitz Jan 31 '12 at 19:53
1

Are the columns delimited by tabs?

Look at Java CSV, an open source library for reading comma delimited or tab delimited text files. SHould do most of the job. I've never used it myself, but I assume you'd be able to ask for all the values from column 1 (or similar).

Alternatively, you could read the file one line at a time using a BufferedReader (which has a readLine()) method) and then call String.split() and grab the parts you want.

user949300
  • 15,364
  • 7
  • 35
  • 66