Results 1 to 3 of 3

Thread: Parsing a line

  1. #1
    reis3k is offline Member
    Join Date
    Aug 2009
    Posts
    12
    Rep Power
    0

    Default Parsing a line

    Hello,
    I have such a line:
    Java Code:
    2009-08-06T13:20:29.418Z   301        312 http:\\xyy.de/robots.txt P http:\\xxx.de/ text/html #003 20090806132028940+466 sha1:ZUBB32ARRB36PFMLJCG3RT4BL3JZ35HG - -
    and I want to parse this line into tokens e.g. it will result like this:
    Java Code:
    1) 2009-08-06T13:20:29.418Z
    2) 301
    3) 312
    4) http:\\xyy.de/robots.txt P http:\\xxx.de/
    5) P
    6) http:\\mpi-inf.mpg.de/
    7) text/html
    8) #003
    9) 20090806132028940+466
    10) sha1:ZUBB32ARRB36PFMLJCG3RT4BL3JZ35HG
    11) -
    12) -
    However, tokens are not separated with tab because they are right aligned. Therefore, I thought that simple idea should be that line should be divided into its tokens in a way that after some space and then when encountered a string or integer, it should be tokenized.

    Actually, this will my first java program. It will read a log file from file and analyze it in above way. Here is the part to read the file, iwill continuw to this code:
    Java Code:
    package org.ReadMe;
    import java.io.*;
    
    public class ReadMe {
    	 public static void main(String args[])
    	  {
    	      try{
    	    // Open the file that is the first 
    	    // command line parameter
    	    FileInputStream fstream = new FileInputStream("crawl.log");
    	    // Get the object of DataInputStream
    	    DataInputStream in = new DataInputStream(fstream);
    	        BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	    String strLine;
    	    //Read File Line By Line
    	    while ((strLine = br.readLine()) != null)   {
    	      // Print the content on the console
    	      System.out.println (strLine);
    	    }
    	    //Close the input stream
    	    in.close();
    	    }catch (Exception e){//Catch exception if any
    	      System.err.println("Error: " + e.getMessage());
    	    }
    	  }
    	}
    Can you show me a method? Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How about just splitting it on whitespace?
    Java Code:
    public class Fu3 {
      public static void main(String[] args) {
        String text = "2009-08-06T13:20:29.418Z   301        312 http:\\xyy.de/robots.txt P http:\\xxx.de/ text/html #003 20090806132028940+466 sha1:ZUBB32ARRB36PFMLJCG3RT4BL3JZ35HG - -";
        String regex = "\\s+";
        String[] tokens = text.split(regex);
        for (String token : tokens) {
          System.out.println(token);
        }
      }
    }

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 9
    Last Post: 04-07-2009, 03:51 AM
  2. Xml Parsing
    By Nomad in forum XML
    Replies: 12
    Last Post: 02-22-2009, 11:19 AM
  3. xml parsing
    By gaurav65176 in forum XML
    Replies: 5
    Last Post: 11-12-2008, 11:07 AM
  4. Parsing URL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:16 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

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