Applying Regular Expressions on the Contents of a File

This section illustrates you how to search a string in a file content.

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

Applying Regular Expressions on the Contents of a File

     

This section illustrates you how to search a string in a file content. Following takes the file name from which the given string/text has to be searched. File name must be text file (with ".txt" extension). This program is followed only for the text file.

Code Description:

FileChannel:
This is the class of java.nio.channels.*; package. This class is used for reading, writing and manipulating a file. Multiple threads use the file channel concurrently. And file channel is safe for that purposes.

new FileInputStream(filename).getChannel():
Above method returns the unique instance of the FileChannel which is associated with the object of the FileInputStream class. The constructor of the FileInputStream class take a file name for which the object of the FileChannel has to be created.

CharBuffer:
This is the class of the java.nio package. This class is used for performing many type of operation on the character buffer. Here in the following program, the object of the CharBuffer class has been created by using the forname() method of the Charset class.

Charset:
This is the class of java.nio.charset package which provides the facilities for mapping between 16-bit Unicode characters sequences and the sequences of bytes. This class is also used for creating decoders and encoders for reading, writing and manipulating files. It defines a static method for check whether the specified charset is supported or not for the JVM (Java Virtual Machine).

Charset.forname("8859_1").newDecoder().decode(fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)fileChannel.size())):
Above code creates a instance of the CharBuffer class. This code has been used many other methods and fields as parameter of the forname() method of the Charset class. This method takes a charset specified as "8859_1".

Charset.newDecoder():
Above method of the Charset class creates a decoder for the specified charset.

Charset.decode():
Above method of the Charset class is used to decode bytes of the specified charset into Unicode characters.

Here is the code of the program:

import java.util.regex.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;

public class SearchFromFile{
  public static void main(String[] argsthrows IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter file name for search: ");
  String filename = in.readLine();
  File file = new File(filename);
  if(!filename.endsWith(".txt")){
  System.out.println("This is not a text file.");
  System.out.println("Please enter file name withe \".txt\" extension.");
  System.exit(0);
  }
  else if(!file.exists()){
  System.out.println("File not found.");
  System.exit(0);
  }
  
  FileChannel fileChannel = new FileInputStream(filename).getChannel();
  CharBuffer charBuffer = Charset.forName("8859_1").newDecoder().decode(
fileChannel.map
(FileChannel.MapMode.READ_ONLY, 0(int)fileChannel.size()));
  System.out.print("Enter string to search from the file: ");
  String string = in.readLine();
  Pattern pattern = Pattern.compile(string);
  Matcher matcher = pattern.matcher(charBuffer);
  
  int a = 0;
  while(matcher.find()){
  a++;
  String matchStrings = matcher.group();
  System.out.println(matchStrings);
  }
  System.out.println("Total occurance of the word in the file is: " + a);
  }
}

Download this example.

Advertisements

Share on Google+Share on Google+

Applying Regular Expressions on the Contents of a File

Posted on: April 16, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Applying Regular Expressions on the Contents of a File   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads