Files and Java I/O

0
129

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

[java] import java.io.*;
class Fan{
public static void main(String args[]){
speed{
FileOutputstream fout=new FileOutputStream(“fmn.txt”);
String s=”Fan moves very fast”;
byte b[]=s.getBytes();
fout.write(b);
fout.close();
System.out.println(“DONE”);
}catch(Exception e){system.out.println(e);}
}
}

[/java] Output : DONE

Example of Input stream

[java] import java.io.*;
class Fan{
public static void main(String args[]){
speed{
FileInputStream fin=new FileInputStream(“fmn.txt”);
int i=0;
while((i=fin.read())!=-1){
System.out.println((char)i);
}
fin.close();
}catch(Exception e){system.out.println(e);}
}
}
[/java] Output: Fan moves very fast

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.