IO Streams PDF
IO Streams PDF
IO Streams PDF
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
1
JAVA Means DURGASOFT
IOStreams
In any programming language,in any application,providing input to the
programme and getting output from the programme is essential.
In case of C and C++ applications,we are able to perform input and output
operations by using some predefined library in the form of
printf(),scanf(),cin>>,cout<<,......
Java has represented all the streams in the form of predefined classes in
"java.io" package.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
2
JAVA Means DURGASOFT
2.Character-Oriented Streams
1.Byte-Oriented Streams
These are Streams,which will allow the data in the form of bytes from input
devices to Java program and from java program to output devices.
1.InputStream
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
3
JAVA Means DURGASOFT
2.OutputStream
1.InputStream:
It is a byte-oriented Stream,it will allow the data in the form of bytes from
input devices to Java program.
Ex:
ByteArrayInputStream
FilterInputStream
DataInputStream
ObjectInputStream
FileInputStream
StringBufferInputStream
BufferedInputStream….
2.OutputStream:
It is a byte-oriented Stream,it will allow the data in the form of bytes from
Ex:ByteArrayOutputStream
FilterOutputStream
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
4
JAVA Means DURGASOFT
DataOutputStream
FileOutputStream
PrintStream
BufferedOutputStream..
2.Character-Oriented Streams:
These are the Streams,which will allow the data in the form of characters
from input devices to java program and form java program to output
devices.
1.Reader
2.Writer
1.Reader:
Ex:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
5
JAVA Means DURGASOFT
CharArrayReader
FilterReader
BufferedReader
FileReader
InputStreamReader….
2.Writer:
Ex:
CharArrayWriter
FilterWriter
FileWriter
PrintWriter
BufferedWriter….
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
6
JAVA Means DURGASOFT
FileOutPutStream:
To transfer the data from Java program to a particular target file by using
FileOutPutstream we have to use the following Steps.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
7
JAVA Means DURGASOFT
Ex:
-->It will override the existed data in the target file at each and every write
operation.
-->It will not override the existed data in the target file,it will append the
specified new data to the existed data in the target file.
When JVM encounter the above instruction,JVM will perform the following
tasks.
b)JVM will search for the specified target file at the respective location.
c)If the specified target file is available then JVM will establish
FileOutPutStream from java program to target file.
d)If the specified target file is not available then JVM will create a file with
the target file name and establish FileOutPutStream from Java program to
target file.
String data="Hello";
byte[] b=data.getBytes();
Ex:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
8
JAVA Means DURGASOFT
fos.write(b);
4)Close FileOutPutStream:
fos.close();
2.FileInputStream:
If we want to transfer the data from source file to java programme by using
FileInputStream,we have to use the following Steps:
Ex:
When JVM encounter the above instruction then JVM will perform the
following actions.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
9
JAVA Means DURGASOFT
2.JVM will search for the specified source file at the respective location.
3.If the source file is not available at the respective location then JVM will
raise an excpetion like "java.io.FileNotFoundException".
4.If the required source file is available then JVM will establish
FileInputStream from source file to JAVA program.
5.After creating FileInputStream,JVM will transfer the data from source file
to FileInputStream in the form bytes.
2.Get the size of the data from FileInputStream and prepare byte[] with the data
size:
To get the size of the data from FileInputStream,we have to use the
following method
Ex:
int size=fis.available();
To read the data from FileInputStream into byte[],we have to use the
following method.
Ex:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
10
JAVA Means DURGASOFT
fis.read(b);
System.out.println(data);
5.close FileInputStream:
fis.close();
import java.io.*;
class DisplayEx{
String file_Name=args[0];
int size=fis.available();
fis.read();
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
11
JAVA Means DURGASOFT
System.out.println(data);
fis.close();
}}
import java.io.*;
import java.util.*;
class Word_Count_Ex{
int size=fis.available();
fis.read();
int tokens=st.countTokens();
int count=0;
while(st.hasMoreTokens()){
String token=st.nextToken();
if(token.equals("is")){
count=count+1;
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
12
JAVA Means DURGASOFT
fis.close();
}}
import java.io.*;
int size=fis.available();
fis.read(b);
fos.write(b);
fis.close();
fos.close();
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
13
JAVA Means DURGASOFT
FileWriter:
This character-oriented Stream can be used to transfer the data from Java
Application to a particular target File.
-->It will override the existed content with the new content at each and
every write operation.
-->It will append new content to the existed content available in the file at
each and every write operation.
When JVM encounter the above instructions,JVM will take the specified file
and JVM search for the specified file at the respective location,if the required
target file is available then JVM will establish FileWriter from Java application
to the target file.If the required target file is not available at the respective
location then JVM will create a new file with the same specified file name and
establish FileWriter from Java application to the target file.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
14
JAVA Means DURGASOFT
2.Declare the data which we want to transfer and convert that data into char[]:
String data="Hello";
char[] ch=data.toCharArray();
To write char[] data into FileWriter,we have to use the following method.
Ex:fw.write(ch);
4.Close FileWriter:
fw.close();
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
15
JAVA Means DURGASOFT
import java.util.*;
String data="DurgaSoftwareSolutions";
char[] ch=data.toCharArray();
fw.write(ch);
fw.close();
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
16
JAVA Means DURGASOFT
FileReader:
If we want to transfer the data from a particular source file to Java program
by using FileReader then we have to use the following steps:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
17
JAVA Means DURGASOFT
when JVM encounter the above instruction,JVM will perform the following
steps.
b)JVM will check whether the specified file is available or not at the
respective location.
c)If the specified source file is not available at the respective location then
JVM will rise an exception like "java.io.FileNotFoundException".
d)If the specified file is existed at the respective location then JVM will
establish FileReader from source file to Java program.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
18
JAVA Means DURGASOFT
Repeat the above steps upto all the characters which are available in the
respective source file or upto the end-of-file character i.e "-1".
3.Close FileReader:
fr.close();
import java.util.*;
String data="";
int val=fr.read();
while(val!=-1){
data=data+(char)val;
val=fr.read();
System.out.println(data);
fr.close();
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
19
JAVA Means DURGASOFT
Write a JAVA programme to copy a document from one file to another file by
using character oriented Streams?
import java.io.*;
String data="";
int val=fr.read();
while(val!=-1){
data=data+(char)val;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
20
JAVA Means DURGASOFT
val=fr.read();
char[] ch=data.toCharArray();
fw.write(ch);
fr.close();
fw.close();
}}
1.BufferedReader
2.Scanner
3.Console
1.BufferedReader:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
21
JAVA Means DURGASOFT
To read the data from BufferedReader,we will use the following method
1.readLine()
2.read()
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
22
JAVA Means DURGASOFT
import java.io.*;
String data1=br.readLine();
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
23
JAVA Means DURGASOFT
int data2=br.read();
}}
import java.io.*;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
24
JAVA Means DURGASOFT
String val1=br.readLine();
String val2=br.readLine();
System.out.println("Addition :"+val1+val2);
}}
import java.io.*;
String val1=br.readLine();
String val2=br.readLine();
int f_Val=Integer.parseInt(val1);
int s_Val=Integer.parseInt(val2);
System.out.println("Addition :"+(f_Val+s_Val));
}}
Scanner:
This class is provided by Java in java.util package along with JDK5.0 Version.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
26
JAVA Means DURGASOFT
To read String data as Dynamic input,we have to use the following method.
Console:
This class is provided by Java in java.io package along with JAVA6 Version.
nextXXX() methods]
2.These are not providing security for the data like password data,pin
numbers.....
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
27
JAVA Means DURGASOFT
To get Console object,we have to use the following method from "System"
class.
Ex:Console c=System.console();
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
28
JAVA Means DURGASOFT
import java.io.*;
Console c=System.console();
if(uname.equals("durga")&&upwd.equals("durga")){
System.out.println("Valid User");
else{
System.out.println("InValid User");
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
29
JAVA Means DURGASOFT
At remote machine, we have to get the data from network and convert the
data from system representation to System representation and reconstruct
an object on the basis of data.
----Diagram--------------
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
30
JAVA Means DURGASOFT
Serializable interface is marker interface,it will make eligible any object for
Serialization and Deserialization.
int eno=111;
String ename="AAA";
float esal=5000;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
31
JAVA Means DURGASOFT
3.Create ObjectOutPutStream:
Ex:
Ex:oos.writeObject(e1);
2.Create ObjectInputStream:
Ex:Employee e2=(Employee)ois.readObject();
-->If we serialize any object having static variables then compiler will not
rise any error and JVM will not rise any exception but static variables will not
be listed in the serialized data in the text file.
import java.io.*;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
33
JAVA Means DURGASOFT
class A{
int i=10;
int j=20;
class Test{
A a=new A();
oos.writeObject(a);
}}
Status:"java.io.NotSerializableException".[Exception]
Ex:
import java.io.*;
int i=10;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
34
JAVA Means DURGASOFT
int j=20;
class B extends A{
int k=30;
int l=40;
class Test{
B b=new B();
oos.writeObject(b);
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
35
JAVA Means DURGASOFT
Ex:
import java.io.*;
class A {
int i=10;
int j=20;
int k=30;
int l=40;
class Test{
B b=new B();
oos.writeObject(b);
}}
Ex:
import java.io.*;
String bid;
String bname;
this.bid=bid;
this.bname=bname;
}}
String accNo;
String accName;
Branch branch;
this.accNo=accNo;
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
37
JAVA Means DURGASOFT
this.accName=accName;
this.branch=branch;
}}
String eid;
String ename;
Account acc;
this.eid=eid;
this.ename=ename;
this.acc=acc;
}}
class Test{
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
38
JAVA Means DURGASOFT
oos.writeObject(emp);
}}
Externalization:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
39
JAVA Means DURGASOFT
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
40
JAVA Means DURGASOFT
where ObjectInput will get serialized data from text file to perform
manipulations.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
41
JAVA Means DURGASOFT
---implementation---
---implementation----
}}
Ex:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
42
JAVA Means DURGASOFT
import java.util.*;
import java.io.*;
String eid;
String ename;
String email;
String emobile;
public Employee(){
this.eid=eid;
this.ename=ename;
this.email=email;
this.emobile=emobile;
try{
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
43
JAVA Means DURGASOFT
st1.nextToken();
int no=Integer.parseInt(st1.nextToken());
String mail=st2.nextToken();
st3.nextToken();
String mobile=st3.nextToken();
oop.writeInt(no);
oop.writeUTF(ename);
oop.writeUTF(mail);
oop.writeUTF(mobile);
catch(Exception e){
e.printStackTrace();
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
44
JAVA Means DURGASOFT
eid="E-"+oip.readInt();
ename=oip.readUTF();
email=oip.readUTF()+"@durgasoft.com";
emobile="91-"+oip.readUTF();
System.out.println("Employee Details");
System.out.println("-----------------");
System.out.println("Employee Id : "+eid);
}}
class ExternalizableEx{
emp1.getEmpDetails();
oos.writeObject(emp1);
System.out.println();
Employee emp2=(Employee)ois.readObject();
emp2.getEmpDetails();
Files in Java:
1.Sequential Files
2.RandomAccessFiles
1.Sequential Files:
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
46
JAVA Means DURGASOFT
To get file / directory parent location,we have to use the following method.
To get file / directory absolute path,we have to use the following method.
To check whether the created thing File or not,we have to use the following
method.
To check whether the created thing is directory or not we have to use the
following method.
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
47
JAVA Means DURGASOFT
Ex:
import java.io.*;
class Test{
f.createNewFile();
System.out.println(f.isFile());
System.out.println(f.isDirectory());
f1.mkdir();
System.out.println(f.isFile());
System.out.println(f.isDirectory());
int size=fis.available();
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
48
JAVA Means DURGASOFT
fis.read();
System.out.println(data);
}}
RandomAccessFile:
It is a Storage area,it will allow the user to read data from random positions.
"java.io.RandomAccessFile".
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
49
JAVA Means DURGASOFT
Ex:
import java.io.*;
class Test{
raf.writeInt(111);
raf.writeUTF("Durga");
raf.writeFloat(5000.0f);
raf.writeUTF("HYD");
raf.seek(0);
}}
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
50
JAVA Means DURGASOFT
DURGA SOFTWARE SOLUTIONS ,202 HUDA Maitrivanam, Ameerpet , Hyd. Ph: 040-64512786 Page
51