ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Files and I/O

Java I/O Tutorial
Java I/O (Input and Output) is used to take the input and give the output depending on the input.
Java has its own term stream in order to fetch I/O operation faster. The java.io package consists of all the classes required to have an input and output operations.
We can do file handling in java by JAVA IO API.
Stream
A stream is a collection of data in a particular sequence.In Java a stream consists of bytes.
In java, 3 streams are created for the user by default. All these streams are attached with console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
OutputStream
Java application makes the use of an output stream to write the given data to a destination, it can be anything like file,an array,peripheral device or socket.
InputStream
Java application makes the use of an input stream to read the given data from a source, it can be anything like a file,an array,peripheral device or socket.

Example of output stream

  1. import java.io.*;
  2. class Fan{
  3. public static void main(String args[]){
  4. speed{
  5. FileOutputstream fout=new FileOutputStream("fmn.txt");
  6. String s="Fan moves very fast";
  7. byte b[]=s.getBytes();
  8. fout.write(b);
  9. fout.close();
  10. System.out.println("DONE");
  11. }catch(Exception e){system.out.println(e);}
  12. }
  13. }

Output : DONE

Example of Input stream

  1. import java.io.*;
  2. class Fan{
  3. public static void main(String args[]){
  4. speed{
  5. FileInputStream fin=new FileInputStream("fmn.txt");
  6. int i=0;
  7. while((i=fin.read())!=-1){
  8. System.out.println((char)i);
  9. }
  10. fin.close();
  11. }catch(Exception e){system.out.println(e);}
  12. }
  13. }

Output: Fan moves very fast

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!