Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.