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.
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.
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.
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.
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).
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".
Above method of the Charset class creates a decoder for the specified charset.
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:
Liked it! Share this Tutorial