| | |
read a column of text from a file in java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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 . .
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 . .
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:
If you want COL2
OR
If you have specific length for each value under each column you can use the substring method:
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:
The last one depends on the format of your file. The others are more "free".
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
Java Syntax (Toggle Plain Text)
String [] tokens = lineRead.split(" "); String valueNeeded = tokens[1];
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
Java Syntax (Toggle Plain Text)
String valueNeeded = lineRead.substring(5,9);
Check out my New Bike at my Public Profile at the "About Me" tab
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
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.?
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:
Do you what split does? If you knew, then when you do: fields[0] you print the first value of each line. Example
tokens[0] has value: a
tokens[1] has value: b
tokens[2] has value: c
tokens[3] has value: d
From you pm:
Then do
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
In the last for-loop the if will be executed only ONCE for the line specified: x. And you will print only ONE value.
I don't know why you need this and what you are trying to print:
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
String line="a b c d"; String [] tokens=line.split(" ");
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
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
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.
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
![]() |
Similar Threads
- Trying to write information in a text file -> Array (Java)
- Open In New Window Php (PHP)
- Displaying Images( buffered data ) from the Database using Java (Java)
- cant acess some websites and some programs wont connect to the net. (Viruses, Spyware and other Nasties)
- Trojan Horse - HELP PLEASE (Viruses, Spyware and other Nasties)
- Program, help (Java)
- Help with a reservation program! GUI Messed XD (Java)
- Need Help with ArrayList sorting (Java)
Other Threads in the Java Forum
- Previous Thread: need help in gridBagLayout
- Next Thread: Max size of file you can open in java
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) calculator chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






