Hi,

I have a file from which I want to extract values under a particular column and store them in an array

This is the file.

Could anyone help me to get to read the values under the column k1.
fitk2
Date: 2005-08-26 15:37:10
Study: temp2
Data file: k2tis.dft
Plasma file: plasma.dat
Blood file: plasma.dat
Data range: 0 - 60 min (N=21)
Vb: 0 %
Data was not weighted.

Region K1 K1/k2 Vb% WSS AIC
ref 1 Pl01 0.2997 2.0029e+00 4.0005 0.0021 -1.862e+02
SD . . 0.0001 3.8246e-04 0.0095 . .
tis 1 Pl01 0.2996 3.0032e+00 4.0053 0.0081 -1.577e+02
SD . . 0.0001 9.1205e-04 0.0188 . .
tis 2 Pl01 0.0199 2.0121e+00 4.0016 0.0003 -2.302e+02
SD . . 0.0000 3.4698e-03 0.0030 . .
tis 3 Pl01 0.0199 2.2105e+01 4.0029 0.0003 -2.240e+02
SD . . 0.0000 4.7019e-01 0.0033 . .
tis 4 Pl01 0.0997 5.0142e+00 4.0102 0.0074 -1.595e+02
SD . . 0.0001 5.0674e-03 0.0165 . .
tis 5 Pl01 0.0997 5.2174e+01 4.0150 0.0099 -1.535e+02
SD . . 0.0001 5.5789e-01 0.0185 . .
tis 6 Pl01 0.5986 6.0485e+01 4.0758 0.3057 -8.141e+01
SD . . 0.0006 1.5531e-01 0.1065 . .
tis 7 Pl01 0.5986 1.2199e+02 4.0895 0.3340 -7.955e+01
SD . . 0.0006 5.6544e-01 0.1101 . .

Recommended Answers

All 5 Replies

Unfortunately you will have to read the entire line and then "extract" somehow the value under the column you want.
You can use:

StringTokenizer and loop as many times as you want to get to the column you want.
OR
String.split(" ") which will return an array and you will get the value that is at the specified index: eg:

COL1 COL2 COL3
value1 value2 value3

If you want COL2

String [] tokens = lineRead.split(" ");
String valueNeeded = tokens[1];

OR
If you have specific length for each value under each column you can use the substring method:

COL1 COL2 COL3
1234 1234 1234
34 234 4
1234 4 34

If you know that the first 4 characters will be the first value, the characters from 5 till 8 the second value and 10 to 13 the third value:

String valueNeeded = lineRead.substring(5,9);

The last one depends on the format of your file. The others are more "free".

Hi,

Reading the text file I did like this with help from people.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

class ReadTextFileExample
{
    public static void main(String[] args)
    {
        File file = new File("D:/test.txt");
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;

        try
        {
            reader = new BufferedReader(new FileReader(file));
            String text = null;


            while ((text = reader.readLine()) != null)
            {
                contents.append(text)
                .append(System.getProperty(
                    "line.separator"));

                System.out.println(contents.toString());
            }
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }


    }
}

But this reads the whole file and I want to just read suppose x line only and extract its y word in that line. Could anyone help me with this.

another way I did was using this code

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class MyClass {
public static void main(String[] args) throws IOException  {
        BufferedReader br = new BufferedReader(new FileReader("D:/test.txt"));
        ArrayList lines = new ArrayList();
        for(String line = br.readLine();line != null;line = br.readLine()) {
            String[] fields = line.split(" ");
            System.out.println("fields size is "+fields.length);
            System.out.println("fields value is "+fields[0]);
            lines.add(fields);
        }
        String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
        System.out.println("strings are "+strings[0][0]);
        System.out.println("Lines="+strings.length);
    }

}

It prints lines but still unable to extract one word in the whole line. Could anyone tell me the code for that.?

First:
I don't know why you need this and what you are trying to print:

String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
System.out.println("strings are "+strings[0][0]);
System.out.println("Lines="+strings.length);

Do you what split does? If you knew, then when you do: fields[0] you print the first value of each line. Example

String line="a b c d";
String [] tokens=line.split(" ");

tokens[0] has value: a
tokens[1] has value: b
tokens[2] has value: c
tokens[3] has value: d

From you pm:

could u just write a simple code for example to read from
asdf xcvf ghjk esks
0.21 3.22 1.23 2.45
43.3 3.33 2.21 11.3
2.22 2.44 2.55 3.67

and to get values under ghjk ie 1.23, 2.21, 2.55

Then do

String[] fields = line.split(" ");
System.out.println("fields size is "+fields.length);
System.out.println("fields value is "+fields[2]);
lines.add(fields);

Inside the lines ArrayList you will have the 3rd column of each line.

If you want the y column of the x line as you state in one of your posts then, inside the for loop count the lines with an int variable. ( countLines++; )
When you reach the desired line take the desired column:

//y column
//x line

int count=0;
for(String line = br.readLine();line != null;line = br.readLine()) {
 count++;
 if (count==x) {
  String[] fields = line.split(" ");
  System.out.println("fields size is "+fields.length); 
  System.out.println("fields value is "+fields[y-1]);
 }
}

In the last for-loop the if will be executed only ONCE for the line specified: x. And you will print only ONE value.

commented: Good post. +3

A file which containing comma seprated lines in which extract the column by user given
the no of column ,col no are take in dot properties file.
Or changes in dotr properties file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.