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: How to Read a Portion of a File in Java?

  1. #1
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to Read a Portion of a File in Java?

    Hi folks,

    Output Should be:

    Date Bill Bill DateDescript Frm To Dist pur Qty @ Amt Rmk

    01/02/08 256 01/02/08 Ring Hyd Bng 100 xyz 100 abcd 100 top
    02/05/08 325 05/02/08 paper Chnn Bng 25 offi 850 kir 1000 good
    11/05/08 898 21/08/08 pen Hyd Pun 2532 Offi 200 Abv 500 Bad
    11/02/08 998 21/08/08 Pencil Mum Bng 258 Off 205 Acc 600 Good
    02/05/08 968 30/08/08 Bag Hyd Bng 265 Xyz 536 Acc 635 Bad
    Attached Files Attached Files
    Last edited by jazz2k8; May 19th, 2008 at 05:11 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: How to Read a Portion of a File

    Hey jazz2k8,

    I would suggest using Regular expressions to do this.

    This should get you started:

    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Expense {
     
     public static String inputFile = "expense.txt";
     
     public void formatFile() {
     
      try
      {
      FileInputStream in = new FileInputStream(inputFile);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
     
      String regEx1 = "([0-9]/)"; 
      Pattern regEx1P = Pattern.compile(regEx1);
     
      System.out.println("Date     " + "Bill   " + "BillDate " + "Descript" + " Frm   " +
        "To  " + "Dist   " + "pur   " + "Qty " + " @  " + "Amt    " + "Rmk");
     
      while((strLine = br.readLine())!= null)
      {   
       Matcher match1 = regEx1P.matcher(strLine);
     
       if (match1.find())
       {
        System.out.println(strLine);
       }
     
       //System.out.println(strLine);
      }
      }
      catch (Exception e)
      {
       System.out.println("Ouch! " + e);
      }
     }
     
     public static void main(String[] args) {
      Expense exp = new Expense();
      exp.formatFile();
     
     }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to Read a Portion of a File

    Thanks alot PF.....

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: How to Read a Portion of a File

    Please PF if you make comments for this sample.
    Thanks for your help

Similar Threads

  1. Java program to read last line of a file
    By JavaPF in forum File Input/Output Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. 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, 06:32 AM