Program showing creating, reading and writing files in Java


Files also plays important role in any program. For example , if a program needs a log file to be used for storing the records of all the operations performed , then we can create a file and store at some location so that we can track activities performed by that program.

Java contains a package named Java.io is having multiple classes that are used to perform input output operation of Files.

Stream- a Stream is nothing but a sequence of data.There are two Streams in Java.

Inputstream - to data from Files.

Outputstreams- to write data in Files. Basically there are 3 streams in Java.

Byte Stream- performs I/O of 8 bit bytes.
Character Stream- used to perform input output on 16 bit Unicode characters.
Standard Stream

Byte Stream
The following program shows how to read and write bytes using two classes. FileInputStream and FileOutputStream.

package FilePackage;

import java.io.IOException;
import java.io.*;
public class FileClass {
public static void main(String[] args {
FileInputStream fin=null;
FileOutputStream fout=null;
try
{
fin=new FileInputStream("D:/input.txt");
fout=new FileOutputStream("D:/output.txt");
int n;
while((n=fin.read())!= -1)
{
fout.write(n);
}
}
catch(IOException ex)
{
System.out.println("Exception occured " + ex.toString());
}
finally
{
if(fin!=null)
{
fin.close();
}
if(fout!=null)
{
fout.close();
}
}


Explaination

  • FileInputStream and FileOutputStream are the two basic classes used for reading and writing bytes into files.
  • In above program, we created objects of FileInputStream and FileOutputStream classes and initialized them in try block with the file name provided to constructors.
  • Remember the File from which data needs to be read must be present at particular location. Otherwise program will throw FileNotFoundException
  • Then using while loop we read the input.txt file to the end and write the text into the output file.
  • We used catch block to handle the exception if occurred
  • In finally we closed the open connections to both the streams.



  • Character Stream

    The following program shows how to read and write characters using two classes. FileReader and FileWriter.

    package FilePackage;

    import java.io.IOException;
    import java.io.*;
    public class FileClass {
    public static void main(String[] args {
    FileReader fin=null;
    FileWriter fout=null;
    try
    {
    fr=new FileReader("D:/input.txt");
    fw=new FileWriter("D:/output.txt");
    int n;
    while((n=fr.read())!= -1)
    {
    fw.write(n);
    }
    }
    catch(IOException ex)
    {
    System.out.println("Exception occured " + ex.toString());
    }
    finally
    {
    if(fr!=null)
    {
    fr.close();
    }
    if(fw!=null)
    {
    fw.close();
    }
    }


    Explaination

  • FileReader and FileWriter are the two basic classes used for reading and writing characters into files.
  • In above program, we created objects of FileReader and FileWriter classes and initialized them in try block with the file name provided to constructors.
  • Remember the File from which data needs to be read must be present at particular location. Otherwise program will throw FileNotFoundException
  • Then using while loop we read the input.txt file to the end and write the text into the output file.
  • We used catch block to handle the exception if occurred
  • In finally we closed the open connections to both the streams.



  • Standard Streams

    Like other programming languages , Java also provides mechanism for Standard input , standard output and standard Error.

    Standard Input -Providing data to users program via standard input device i.e Keyboard.
    Standard output- Producing output data provided by input to the output device i.e monitor.
    Standard Error- Outout the Error data to the computer screen using the standard error stream.
    Below Program demonstrate the use of Standard Input.

    import java.io.*;
    public class ReadStream {
    public static void main(String args[]) {
    InputStreamReader cin = null;
    try {
    cin = new InputStreamReader(System.in);
    System.out.println("Enter the characters, press 'q' to quit.");
    char ch;
    do {
    ch = (char) cin.read();
    System.out.print(ch);
    } while(ch != 'q');
    catch(IOException ex) { System.out.println("Exception occurred" + ex.toString());
    }
    }finally {
    if (cin != null) {
    cin.close();
    }
    }
    }
    }


    Please write your comment below
    Program showing creating, reading and writing files in Java Program showing creating, reading and writing files in Java Reviewed by LanguageExpert on July 08, 2018 Rating: 5

    No comments