Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Read lines with specific characteristics from a file, exclude all others line.

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read lines with specific characteristics from a file, exclude all others line.

    Hi I am trying to write method to read lines with some specification from a file. for Example,

    my text file contains following:--

    12-01-01 13:26 San Jose 12.99 DVD
    12-12-30 09:40 Miami 13.50 Music
    14-08-30 10:20 Arizona 16.03 Scientist
    11-07-10 09:10 New York 25.00 ColdPlay
    14-08-30 10:20 Arizona 18.04 MeetYou
    14-08-30 10:20 Arizona 50.03 Scientist
    11-07-10 09:30 New York 25.00 ColdPlay
    11-07-10 09:20 New York 25.00 ColdPlay

    tab separated values, for different columns and these are the lines only I want method to read.
    Now suppose if any is there as below, or even enter

    12-01-01 13:26 San Jose 12.99 DVD
    12-12-30 09:40 Miami 13.50 Music
    14-08-30 10:20 Arizona 16.03 Scientist
    11-07-10 09:10 New York 25.00 ColdPlay
    14-08-30 10:20 Arizona 18.04 MeetYou
    [new lines]
    14-08-30 10:20 Arizona 50.03 Scientist
    11-07-10 09:30 New York 25.00 ColdPlay
    //This line should not be read
    even this should not be read #$%^&
    11-07-10 09:20 New York 25.00 ColdPlay

    That particular line should be escaped. Till now I have done when the file format is proper, and it is as below:--

     
    public static void main(String[] args) {
    		 BufferedReader br = null;
    		 String temp = null;
    		 List<String> arrayRead = new ArrayList<String>();
    		 try{
    			 br = new BufferedReader(new FileReader("D:\\testing\\SalesData.txt"));
    			 while((temp=br.readLine())!= null){
    				 arrayRead.add(temp);
    			 }
    			 int n = arrayRead.size();
    			 System.out.println("No. of Records in file "+n);
    			//Add arrayList data to String Array
    			 String[] linesToRead = arrayRead.toArray(new String[arrayRead.size()]);
     
    			 String[] lineX = null;
    			 Hashtable<String, String> dataReq = new Hashtable<String, String>();
    			 for(int i=0; i<arrayRead.size(); i++){
    				 lineX = linesToRead[i].split("\\t");
    				 dataReq.put(lineX[2], lineX[3]);
    			 }
     
    		 }
    		 catch(FileNotFoundException f){
    			 f.printStackTrace();
    		 }
    		 catch(IOException e){
    			 e.printStackTrace();
    		 }
    		 finally{
    			 if(br!= null){
    				 try {
    					br.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    			 }
    		 }
    	}

    Any help will be useful.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    646
    My Mood
    Sleepy
    Thanks
    10
    Thanked 95 Times in 88 Posts

    Default Re: Read lines with specific characteristics from a file, exclude all others line.

    There are at least two possible solutions. a) read the line and use a regex to find out whether you add it to the list. b) read every line and add all of them to the list. Later when you parse the individual parts of a line you ignore those which are unparseable.

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    My Mood
    Cheerful
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read lines with specific characteristics from a file, exclude all others line.

    Quote Originally Posted by PhHein View Post
    There are at least two possible solutions. a) read the line and use a regex to find out whether you add it to the list. b) read every line and add all of them to the list. Later when you parse the individual parts of a line you ignore those which are unparseable.
    Yes.. for the solution a) could you provide me code please, how to use regex

  4. #4
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: Read lines with specific characteristics from a file, exclude all others line.

    Quote Originally Posted by rjshrd View Post
    how to use regex
    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

Similar Threads

  1. File reading from a specific point on a line
    By lordofrandom in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 13th, 2013, 11:25 PM
  2. search for specific line in the file
    By Rajitha in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 16th, 2012, 10:18 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 08:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 07:32 AM