Code to read a file and get the file with a certain pattern


package edu.scjp.regex;

import java.io.*;
import java.util.regex.*;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class FindFiles {
     
      @SuppressWarnings("deprecation")
      public static void main(String args[]){
           
            // variable to hold the file data
            String data = null;
            String[] line = null;
           
            Calendar cal = new GregorianCalendar();
           
            int currentYear =  cal.get(Calendar.YEAR);
          int currentDate =  cal.get(Calendar.DATE);
            int currentMonth = cal.get(Calendar.MONTH);
           
            //String sCurrentDate = currentDate.t
            String currentDateFormat_1 = Integer.toString(currentMonth)+ Integer.toString(currentDate)+ Integer.toString(currentYear);
            String currentDateFormat_2 = Integer.toString(currentYear)+ Integer.toString(currentMonth)+ Integer.toString(currentDate);
           
            System.out.println(currentDateFormat_1);
            System.out.println(currentDateFormat_2);
           
            try {
                  // create a file reader handle
                  FileReader fileReader = new FileReader("C:\\view\\test.txt");
                 
                  BufferedReader bufferedReader = new BufferedReader(fileReader);
                 
                  while((data = bufferedReader.readLine())!= null)
                  {
                        line = data.split(",");
                       
                        for (int i = 0 ; i < line.length; i++)
                        {
                              Pattern pattern_1 = Pattern.compile("CTRAX_NL_SCAN_Roanoke_"+currentDateFormat_1);
                              Pattern pattern_2 = Pattern.compile("NEW_FORMATTED_ROANOKE_"+currentDateFormat_2);
                              Pattern pattern_3 = Pattern.compile("PAID_FORMATTED_ROANOKE_"+currentDateFormat_2);
                             
                              Matcher     matcher_1 = pattern_1.matcher(line[i]);
                              Matcher     matcher_2 = pattern_2.matcher(line[i]);
                              Matcher     matcher_3 = pattern_3.matcher(line[i]);
                       
                       
                        while(matcher_1.find())
                        {
                              System.out.println(line[i]);
                        }
                       
                        while(matcher_2.find())
                        {
                              System.out.println(line[i]);
                        }
                       
                        while(matcher_3.find())
                        {
                              System.out.println(line[i]);
                        }
                  }
                  }    
                 
                  }catch(IOException ioe) {
                  ioe.printStackTrace();
            }
     
      }
}


No comments: