bing
Flat 10% & upto 50% off + Free additional Courses. Hurry up!

Java File I/O

 

Java input and output process the input and produces the output based on the input. Java uses stream to perform input and output operation. Package java.io contains the classes which are needed to input and output operation. Stream is a sequence of data. In java there are three streams are created for us automatically which are as follows:

1. System.in: It stands for standard input stream. It gives the data to user’s program and usually a keyboard is used as standard input stream.

2. System.out: It stands for standard output stream. It is used to output the data produced by the user’s program and usually a monitor is used to standard output stream.

3. System.err: It stands for standard error stream. It is used to output the error data produced by the user’s program.

 

There are two types of streams:

1. Input Stream – It reads data from the source.

input stream

2. Output Stream – It writes data to the destination.

output stream

FileInputStream and FileOutputStream :

These classes are used to perform the read data from the files and write data in the file.

 

FileOutputStream class: This class is used to write the data into the file.

import java.io.*;

public class Intellipaat{

public static void main(String args[]){

try{

FileOutputstream fo=new FileOutputStream("hello.txt");

String i="Hello Intellipaat ";

byte b[]=i.getBytes();/ /converting string into byte array

fo.write(i);

fo.close();

}

catch(Exception e){system.out.println(e);

}

}

}

It will print Hello Intellipaat in file hello.txt.

 

FileInputStream class: This class is used to read the data from the file.

e.g.

import java.io.*;

class ReadHello{

public static void main(String args[]){

try{

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

int i=0;

while((i=fin.read())!=-1){

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

}

fin.close();

}catch(Exception e){system.out.println(e);}

}

}

Output

Hello Intellipaat

Learn more about Cross-Platform Mobile Technology for Android and iOS using Java in this insightful blog now!

"0 Responses on Java File I/O"

Training in Cities

Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

top

Sales Offer

  • To avail this offer, enroll before 05th December 2016.
  • This offer cannot be combined with any other offer.
  • This offer is valid on selected courses only.
  • Please use coupon codes mentioned below to avail the offer
offer-june

Sign Up or Login to view the Free Java File I/O.