Write a Program to perform basic IO operations in java


Write a Program to perform basic IO operations

Problem Statement :

Take file name as input to your program, If file is existing the open and display contents of the file. After displaying contents of file ask user – do you want to add the data at the end of file. If a user gives yes as response, then accept data from user and append it to file. If file in not existing then create a fresh new file and store user data into it. User should type exit on new line to stop the program

Introduction

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, object, localized characters, etc. Stream A stream can be defined as a sequence of data. There are two kinds of Streams:  InPutStream: The InputStream is used to read data from a source. OutPutStream: The OutputStream is used for writing data to a destination.


Java provides strong but flexible support for I/O related to files and networks but this tutorial covers very basic functionality related to streams and I/O. We will see the most commonly used examples one by one: Byte Streams Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream

All the programming languages provide support for standard I/O where the user's program can take input from a keyboard and then produce an output on the computer screen. If you are aware of C or C++ programming languages, then you must be aware of three standard devices STDIN, STDOUT and STDERR. Similarly, Java provides the following three standard streams:

 Standard Input: This is used to feed the data to user's program and usually a keyboard is used as standard input stream and represented as System.in. 

Standard Output: This is used to output the data produced by the user's program and usually a computer screen is used for standard output stream and represented as System.out. 

Standard Error: This is used to output the error data produced by the user's  program and usually a computer screen is used for standard error stream and represented as System.err.

 

 

 

 

Program

import java.util.*;

import java.io.*;

import java.lang.ArrayIndexOutOfBoundsException;

class filehand

{

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

{

int i;

FileInputStream f;

      try

      {

                  f=new FileInputStream("hello.txt");

                  do

      {

                  i=f.read();

                  if(i!=-1)

                  {

                              System.out.print((char)i);

                  }

      }while(i!=-1);

                 

                  f.close();

      }

      catch(FileNotFoundException e)

      {

                  System.out.println("File not found");

                  return;

      }

      catch(ArrayIndexOutOfBoundsException e)

      {

                  System.out.println("showfile");

      }

}

}

Post a Comment

Previous Post Next Post