You are here:   HomeJava ProgrammingStreamWrite Data to a File Using Scanner in Java Example
by Dinesh Thakur Category: Stream

The File class explains only the file system. Java provides two special types of stream called the File input Stream and File Output Stream to read data from and write data into the file. These classes operate on the files in the native file system. File input Stream and File Output Stream are subclasses of Input Stream and Output Stream, respectively. Usually, we use File input Stream(for byte streams) or File Reader (for character streams) for reading from a file and File Output Stream(for byte streams) or File Writer (for character streams) for writing into a file.

 

We can create a file stream by giving the file name as a parameter in the form of a string, File object or File Descriptor object. File Writer and File Output Stream will create a new file by that name if the file does not already exist.

The constructors of these two streams are:

• FileinputStream(String filename);    • File Output Stream(String filename);

• FileinputStream(File file object);       • File Output Stream(File file object);

• FileinputStream(File Descriptor fdesobject);  • File Output Stream(File Descriptor

                                                                              fdes object);

File Descriptor is an object that holds the information about a file.

A File input Stream object can be created in the following manner:

                        File input Stream fin = new File input Stream(string filename);

Alternatively, it can be created with the following set of statements:

                      File = new File(string filename);

                      File input Stream fin = new File input Stream(f);

A File Output Stream object can be created as follows:

                 File Output Stream foot = new File Output Stream(string filename);

Note that in the case of File Output Stream if it is opened on a file that does not already exist then a new file by that name is created.

Program  implementing the class Copy File uses File input Stream and File Output Stream to copy the contents of input File to output File: input File and output File are the command line arguments arg[0] and arg[1].

 Using Copy File, File input Stream and File Output Stream.

import java.io.*;

public class Copy File

{

      public static void main(String[] args) throws IO Exception

     {

            if(args.length<2)

            System.out.println("\n No sufficient parameters");

            else

            {

                File input File = new File(args[0]);

                File output File = new File(args[1]);

                FileinputStream in = new FileInputStream(inputFile);

                FileOutputStream out = new FileOutputStream(outputFile);

               int c;

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

                out. write(c);

                   in.c1ose();

                   out.c1ose();

             }

       }

}

In Program the file input File is opened using File input Stream and another file output File is opened using File Output Stream. If input File does not exist, the program gives an error because File input Stream cannot work if the file does not exist. On the other hand, if output File does not exist, then a file by that name will be created. The program continues to read characters from File input Stream as long as there are inputs in the input file and writes these characters on to File Output Stream. When all the inputs have been read, the program closes both File input Stream and File Output Stream. Program  can also be written using File Reader and File Writer with the small changes shown below:

                                         File inputFile = new File(args[0]);

                                         FileReader in = new FileReader(input File);

This code creates a File object that represents the named file on the native file system. File is a utility class provided by java.io. Program the Copy File program uses this file object only to construct a File Reader on a file; however, the program could also use the input file to get information about the file, such as its full path name. If we run Program an exact copy of input File (args[0]) will be found in a file named output File (args[1]) in the same directory. It should be remembered that File Reader and File Writer read and write 16-bit characters; however, most native file systems are based on 8-bit bytes. These streams encode the characters as they operate according to the default character-encoding scheme. The default character-encoding scheme can be found by using System. Get Property("file. Encoding"). To specify an encoding scheme other than the default, we should construct an Output Stream Writer on a File Output Stream and specify the encoding scheme. It is important to note that the File Output Stream object does not support the means to append file. If the file exists already, File Output Stream only overwrites and cannot add content to amend of the file.