• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Stream » File input Stream and File Output Stream
Next →
← Prev

File input Stream and File Output Stream

By Dinesh Thakur

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.

You’ll also like:

  1. Console Input-Output in C#
  2. Input/Output Operator in C++
  3. Input Output Statements in c++
  4. Input and Output Devices of Computer
  5. What is BIOS (basic input/output system)?
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW