What is an I/O filter in Java?


This article will help you understand what I/O filter in Java is.

The Java I/O Filter

The Java I/O Filter is inside java.io package. It provides sets of input and output streams used for reading and writing data to input and output sources. There are different types of classes in java.io, naming Input Stream, Output Stream, etc. Some of the important types are discussed below −

Input Stream

The InputStream class of java.io is an abstract superclass which reads data from an input source. The source can be a file, a string, or anything that can contain data. This class is a programming interface for reading array of bytes, marking locations in the stream, skipping input bytes, determining number of readable bytes and resetting current position within the stream. While creation, an input stream is automatically opened. One can close a stream manually using the clone() method, or can close it by default when the object is garbage collected.

Some methods included in the InputStream class are −

  • read()

  • read(byte[] array)

  • available()

  • mark()

  • reset()

  • markSupported()

  • skips()

  • close()

Syntax

InputStream object = new FileInputStream();

Example

File.txt Hello Readers!! Welcome to Java.

import java.io.FileInputStream; // importing Input Stream class in java.io package import java.io.InputStream; class InputStreamExample { // class declaration public static void main(String args[]){ // main function declaration byte[] array = new byte[100]; // byte array initialization try {// try block InputStream input = new FileInputStream("File.txt"); // Input Stream class object created System.out.println("Available bytes in the file: " + input.available()); input.read(array); // reading byte from input stream System.out.println("Data read from the file: "); String data = new String(array); // converting byte array into string System.out.println(data); input.close(); // closing the input stream } catch (Exception e) { // catch block e.getStackTrace(); } } }

Output

Available bytes in the file: 32
Data read from the file:
Hello Readers!! Welcome to Java.

Output Stream

The OutputStream class of java.io is an abstract superclass which writes data to an output source. The source can be a file, a string, memory or anything that can contain data. It is a sibling to InputStream and can write data that is readable by InputStream. This class is a programming interface for writing array of bytes to the stram and flushing the stream. While creation, an output stream is automatically opened. One can close a stream manually using the clone() method, or can close it by default when the object is garbage collected.

Some methods included in the OutputStream class are −

  • write()

  • write(byte[] array)

  • flush()

  • close()

Syntax

OutputStream object = new FileOutputStream();

Example

import java.io.FileOutputStream; // importing Output Stream class in java.io package import java.io.OutputStream; public class OutputStreamExample { // class declaration public static void main(String args[]) { // main function declaration String data = "Hello Readers!! Welcome to TutorialsPoint"; // string variable storing line to be written in File.txt try { // try block OutputStream out = new FileOutputStream("File.txt"); // Output Stream class object created byte[] dataBytes = data.getBytes(); // Converts the string into bytes out.write(dataBytes); // Writes data to the output stream System.out.println("Data is written to the file."); out.close(); // Closes the output stream } catch (Exception e) { // catch block e.getStackTrace(); } } }

Output

Data is written to the file.

File

The File class is an abstract version of a file with directory path name. A path name can be relative or absolute in nature. There exists different methods for working with directories and files like create(), delete(), rename() etc.

Example

import java.io.*; // importing java.io package public class FileExample { // class declaration public static void main(String[] args){ // main function declaration try { // try block File file = new File("File1.txt"); // creating new file if (file.createNewFile()){ // checking if new file is created or not System.out.println("New File is created!"); } else { System.out.println("File already exists."); } } catch (IOException e) { // catch block e.printStackTrace(); } } }

Output

New File is created!

RandomAccessFile

RandomAccessFile class is used to read and write on a random access file. A random access file has same behavioral characteristics as that of a large array of bytes. The file pointer can be called as a cursor which is used for read write operations. If the control reaches the end of the file before reading the desired number of bytes, then an EOFException is thrown.

StreamTokenizer

The StringTokenizer class is used to break a String into smaller parts known as tokens. It is one of the Legacy classes of Java. It doesn't provide the facility to differentiate numbers, quoted strings, identifiers etc. For example −

Given String − Hi Readers of this Article

After using String Tokenizer, the Tokens are: Hi, Readers, of, this, Article.

There are 3 types of Constructors defined in String Tokenizer class. They are −

  • StringTokenizer (String str) This creates StringTokenizers with specified string.

  • StringTokenizer (String str, String delim) This creates StringTokenizers with specified string and delimiter. Delimiter are characters that split (separate) the string into tokens.

  • StringTokenizer (String str, String delim, boolean returnValue) This creates StringTokenizer with specified string, delimiter and returnValue. If return value is true or 1, delimiter characters are considered to be tokens. In case if return value is false or 0, delimiter characters split the string into separate tokens.

Updated on: 05-Sep-2022

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements