Results 1 to 15 of 15
  1. #1
    miladirooni is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default 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

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What ideas do you have for it so far?

  3. #3
    miladirooni is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    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();

    }
    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are using the wrong tools. Use FileReader/BufferedReader combination to read character files.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    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
    ?

  7. #7
    miladirooni is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    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;

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    ...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.

  10. #10
    miladirooni is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    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);
    }
    }
    }

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    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.

  12. #12
    miladirooni is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    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?:-(

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    Formatted a bit nicer. You need to use CODE tags as otherwise it's almost impossible to figure out flow.

    Right...

    Java Code:
    public class readingcif {
    
    	public static void main(String args[]) {
    		read("C1-300K ActaB63 page850.cif");
    	}
    (Classnames should start with a capital letter).
    Here you call your read method, passing in the file name.
    Good so far.

    Java Code:
    	public static void read(String R) {
    
    		try {
    			FileReader fr = new FileReader(R);
    			BufferedReader br = new BufferedReader(fr);
    (Variable names should start with a lower case letter unless static final).
    Open the readers, all well and good.

    Java Code:
    			String s = R;
    Um...why store the filename in here?
    Java Code:
    			String tokens[] = null;
    			String splitPattern = "_pd_proc_intensity_bkg_calc";
    What is this pattern for?
    Java Code:
    			tokens = s.split(splitPattern);
    			for (int i = 0; i < tokens.length; i++) {
    				prt(tokens[i]);
    			}
    And why on earth are you using it to split the filename?
    And then loop round the (lone) token?
    Java Code:
    			while ((R = br.readLine()) != null) {
    				prt(R);
    			}
    R is your filename. Why are you reusing the same variable?
    Java Code:
    			fr.close();
    		} catch (Exception e) {
    			prt("Exception: " + e);
    		}
    and close...OK (should be in a finally block, but I'll let you off that for the moment).
    Java Code:
    	}
    
    	public static void prt(String s) {
    		System.out.println(s);
    	}
    }
    So, first off, break this down. Do as I wrote above.

    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*.

  14. #14
    miladirooni is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    Tanx Tolls 4 ur help but I gave need to learn much more about Java...

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    You really ought to use whole words...I'm too old for text speak.
    :)

Similar Threads

  1. reading from a .txt
    By Flamespewer in forum New To Java
    Replies: 1
    Last Post: 09-14-2009, 08:35 AM
  2. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  3. Right use of file reading ?
    By jurka in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 08:16 PM
  4. Reading API for ArrayList
    By kian_hong2000 in forum New To Java
    Replies: 10
    Last Post: 08-22-2008, 06:09 PM
  5. Reading Pen Drive
    By ravjot28 in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2008, 02:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •