Files and Input output stream in java

In Java, there is a package called java.io which contains nearly every class you might ever need to perform input and output (I/O). And these streams basically represent an input source and output destination. It supports many data such as primitives, objects, localized characters, .etc.

Stream in Java

In java.io a stream can be directly defined as a sequence of data. There are two types of Stream.

  • InPutStream: This stream basically used to read data from a source.
  • OutPutStream: This stream basically used to write data to a destination.

As you know java provides very strong and flexible support for I/O related to files and networks but this article we only cover very basic functionality related to streams and I/O which we most used one by one −

Byte Streams

Byte streams in java are used to perform input and output tasks of 8-bit bytes. Basically there are so many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream. Below is an example which makes use of these two classes to copy an input file into an output file let’s check it out !!

import java.io.*;

public class JustCopyFile {

    // just create a class name JustCopyFile

   public static void main(String args[]) throws IOException {  

      FileInputStream in = null;

      FileOutputStream out = null;


      try {

         in = new FileInputStream("inputs.txt");

         out = new FileOutputStream("outputs.txt");

         

         int c;

         while ((c = in.read()) != -1) {

            out.write(c);

         }

      }finally {

         if (in != null) {

            in.close();

         }

         if (out != null) {

            out.close();

         }

      }

   }}

Now let’s try writing in file inputs.txt with the below contents −

This is a test for the file for testing copy program.

As a next step to compile the above program and execute it !! By using

$ javac JustCopyFile.java

$ java JustCopyFile

FileInputStream

This stream basically used for reading data from the files. And objects can be created using the keyword new and there are several types of constructors available.

Below constructor takes a file name as a string to create an input stream object to read the file −

InputStream sr = new FileInputStream("C:/java/code");

Below constructor takes a file object to create an input stream object to read the file. First, we need to create a file object using File() method as follows −

File sr = new File("C:/java/code");

InputStream sr = new FileInputStream(sr);

After an InputStream object created, then there is some helper methods that can be used to read to stream or to do other operations on the stream.

1). public void close() throws IOException{}: By this method closes the file output stream. And just releases any system resources associated with the file. It also throws an IOException if anything wrong.

2). public int available() throws IOException{}: It gives the number of bytes that can be read from this file input stream. Returns an int that’s it.

3). public int read(byte[] r) throws IOException{}: Basically, this method reads r.length bytes from the input stream into an array and returns the total number of bytes read. If it is the end of the file, -1 will be returned not 1.

FileOutputStream

This stream is used to create a file and write data into it. And create a file, if it doesn’t already exist, before opening it for output.

Here are mainly two constructors that can be used to create a FileOutputStream object.

Below constructor takes a file name as a string to create an input stream object to write the file −

OutputStream sr = new FileOutputStream("C:/java/code")

Below constructor takes a file object to create an output stream object to write the file. First, we need to create a file object using File() method as follows −

File sr = new File("C:/java/code");

OutputStream sr = new FileOutputStream(sr);

Directories in Java

Basically directory is a File which can contain a list of other files and directories. Mainly, you use File object to create directories, to list down files available in a directory.

For Creating Directories :

You can use mkdir() method for creates a directory or mkdirs() method creates both a directory and all the parents of the directory.