read a column of text from a file in java

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2008
Posts: 3
Reputation: gvarma123 is an unknown quantity at this point 
Solved Threads: 0
gvarma123 gvarma123 is offline Offline
Newbie Poster

read a column of text from a file in java

 
0
  #1
Jun 4th, 2008
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 . .
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,683
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: read a column of text from a file in java

 
0
  #2
Jun 4th, 2008
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
  1. String [] tokens = lineRead.split(" ");
  2. 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:
  1. String valueNeeded = lineRead.substring(5,9);
The last one depends on the format of your file. The others are more "free".
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: gvarma123 is an unknown quantity at this point 
Solved Threads: 0
gvarma123 gvarma123 is offline Offline
Newbie Poster

Re: read a column of text from a file in java

 
0
  #3
Jun 4th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: gvarma123 is an unknown quantity at this point 
Solved Threads: 0
gvarma123 gvarma123 is offline Offline
Newbie Poster

Re: read a column of text from a file in java

 
0
  #4
Jun 4th, 2008
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.?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,683
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: read a column of text from a file in java

 
1
  #5
Jun 4th, 2008
First:
I don't know why you need this and what you are trying to print:
  1. String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
  2. System.out.println("strings are "+strings[0][0]);
  3. 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
  1. String line="a b c d";
  2. 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
  1. String[] fields = line.split(" ");
  2. System.out.println("fields size is "+fields.length);
  3. System.out.println("fields value is "+fields[2]);
  4. 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
  1. int count=0;
  2. for(String line = br.readLine();line != null;line = br.readLine()) {
  3. count++;
  4. if (count==x) {
  5. String[] fields = line.split(" ");
  6. System.out.println("fields size is "+fields.length);
  7. System.out.println("fields value is "+fields[y-1]);
  8. }
  9. }

In the last for-loop the if will be executed only ONCE for the line specified: x. And you will print only ONE value.
Last edited by javaAddict; Jun 4th, 2008 at 9:39 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC