Results 1 to 15 of 15
Thread: Reading and filtring
- 09-16-2009, 11:24 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Reading and filtring
Hi guys
I have a file looks like this:
loop_
_pd_proc_point_id
_pd_proc_2theta_corrected
_pd_proc_intensity_total
_pd_calc_intensity_total
_pd_proc_intensity_bkg_calc
2 4.1649 109009(330) 109137.3360 109008.9920
3 4.2649 113574(337) 113709.5470 113573.9920
4 4.3649 118037(344) 118180.4300 118037.0080
5 4.4649 121799(349) 121951.0310 121799.0080
6 4.5649 126457(356) 126618.4610 126457.0000
7 4.6649 128431(358) 128602.8520 128431.0000
8 4.7649 131309(362) 131492.3590 131309.0000
9 4.8649 133947(366) 134143.2030 133947.0000
what i am trying to do is i need to read this file and filter out the text I only need the last two columns of data,
Does anyone have an idea how to do this?
Many tanx
- 09-16-2009, 12:17 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What ideas do you have for it so far?
- 09-16-2009, 01:49 PM #3
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
thats what i've done so far still stuck dont know what to do...
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.filechooser.FileNameExtensionFilter;
public class ReadFile {
boolean _pd_proc_intensity_bkg_calc=true;
public static void main(String[] args) throws IOException {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAn dFeelClassName());
} catch (Exception e) {
}
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
".cif ", "txt", "cif");
chooser.setFileFilter(filter);
chooser.showOpenDialog(chooser);
File f = chooser.getSelectedFile();
FileInputStream fis = new FileInputStream(f);
byte b = 0;
do {
b = (byte) fis.read();
System.out.print((char) b);
} while (b != -1);
fis.close();
}
}
- 09-16-2009, 02:26 PM #4
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You are using the wrong tools. Use FileReader/BufferedReader combination to read character files.
- 09-16-2009, 04:38 PM #5
For filtering, Java's REGEX package might help. I used it to great advantage when writing a compiler last year. The API pages for Pattern and Matcher in the regex package give nice examples, and I'm sure there are many in the sun tutorials.
- 09-16-2009, 04:57 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
I'd say Regex is a bit OTT for this (bar a split on space).
OP:
Is the format of the file as follows -
-- header names
-- blank line --
-- actual data
?
- 09-17-2009, 09:47 AM #7
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
yeah something similar to that. this file comes from powder diffraction experiment and I only need the filter to columns of the file in order to plot. i have tried another method that reads all the numbers in the files but still cant figure out how to read the two columns only. thats what ive got so fare..
import java.io.FileReader;
import java.io.StreamTokenizer;
public class Tokenizing {
public static void main(String[] argv) throws Exception {
FileReader rd = new FileReader("C1-300K ActaB63 page850.cif");
StreamTokenizer st = new StreamTokenizer(rd);
int token = st.nextToken();
while (token != StreamTokenizer.TT_EOF) {
token = st.nextToken();
switch (token) {
case StreamTokenizer.TT_NUMBER:
double num = st.nval;
System.out.println(num);
break;
- 09-17-2009, 09:51 AM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You can read the file line by line using FileReader/BufferedReader (like I vainly suggested above) and using bufferedReader.readLine() then when you split each line's contents with String.split you will get the values you want as the last and second from last values of the resultant array.
- 09-17-2009, 12:20 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
...and keep reading until you hit the blank line, as that's your cue that you've got past the header information. All the lines after that can be split() since they're all data lines. no need for anything fancy at all, it'll only confuse things.
- 09-17-2009, 12:33 PM #10
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Hi
tanx for replying, i have used FileReader/BufferedReader as u said but how can i use the String.split to filter out the text and some columns?
import java.io.BufferedReader;
import java.io.FileReader;
public class FileReaderLine {
public static void main(String args[]) {
try {
FileReader fr = new FileReader("C1-300K ActaB63 page850.cif");
BufferedReader br = new BufferedReader(fr);
String s;
while ((s = br.readLine()) != null) {
System.out.println(s);
}
fr.close();
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
- 09-17-2009, 12:51 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
So, what you want to do is:
Open the reader (which you're doing)
Read to start of data (ie read until you get the blank line)
Read in data, splitting on the space that appears to separate the columns (or is it a tab?).
Close the reader (which you're doing).
For the split() bit, simply read the api.
- 09-17-2009, 05:49 PM #12
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
hi
this is what ive got so fare :
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class readingcif {
public static void main(String args[]) {
read("C1-300K ActaB63 page850.cif");
}
public static void read(String R) {
try {
FileReader fr = new FileReader(R);
BufferedReader br = new BufferedReader(fr);
String s = R;
String tokens[] = null;
String splitPattern = "_pd_proc_intensity_bkg_calc";
tokens = s.split(splitPattern);
for (int i = 0; i < tokens.length; i++) {
prt(tokens[i]);
}
while ((R = br.readLine()) != null) {
prt(R);
}
fr.close();
} catch (Exception e) {
prt("Exception: " + e);
}
}
public static void prt(String s) {
System.out.println(s);
}
}
but still wont work any idea?:-(
- 09-17-2009, 06:12 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Formatted a bit nicer. You need to use CODE tags as otherwise it's almost impossible to figure out flow.
Right...
(Classnames should start with a capital letter).Java Code:public class readingcif { public static void main(String args[]) { read("C1-300K ActaB63 page850.cif"); }
Here you call your read method, passing in the file name.
Good so far.
(Variable names should start with a lower case letter unless static final).Java Code:public static void read(String R) { try { FileReader fr = new FileReader(R); BufferedReader br = new BufferedReader(fr);
Open the readers, all well and good.
Um...why store the filename in here?Java Code:String s = R;
What is this pattern for?Java Code:String tokens[] = null; String splitPattern = "_pd_proc_intensity_bkg_calc";
And why on earth are you using it to split the filename?Java Code:tokens = s.split(splitPattern); for (int i = 0; i < tokens.length; i++) { prt(tokens[i]); }
And then loop round the (lone) token?
R is your filename. Why are you reusing the same variable?Java Code:while ((R = br.readLine()) != null) { prt(R); }
and close...OK (should be in a finally block, but I'll let you off that for the moment).Java Code:fr.close(); } catch (Exception e) { prt("Exception: " + e); }
So, first off, break this down. Do as I wrote above.Java Code:} public static void prt(String s) { System.out.println(s); } }
Have a method that takes in a filename and opens and closes the file (as you have, but scrap the bits in the middle).
Between opening and closing the file, have it call firstly a method that takes a BufferedReader and reads to the start of the data.
Have a second method that takes the same BufferedReader and will (don't do that bit yet, just leave the method blank) read the data.
And I'll be back in the morning...home to bed, said Zebedee...*boing*.
- 09-18-2009, 10:15 AM #14
Member
- Join Date
- Sep 2009
- Posts
- 9
- Rep Power
- 0
Tanx Tolls 4 ur help but I gave need to learn much more about Java...
- 09-18-2009, 10:35 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
reading from a .txt
By Flamespewer in forum New To JavaReplies: 1Last Post: 09-14-2009, 08:35 AM -
problem with reading excel sheet data reading using poi libraries
By sandeepsai17 in forum New To JavaReplies: 5Last Post: 08-21-2009, 11:03 AM -
Right use of file reading ?
By jurka in forum New To JavaReplies: 3Last Post: 08-27-2008, 08:16 PM -
Reading API for ArrayList
By kian_hong2000 in forum New To JavaReplies: 10Last Post: 08-22-2008, 06:09 PM -
Reading Pen Drive
By ravjot28 in forum Advanced JavaReplies: 1Last Post: 07-05-2008, 02:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks