Input Output
Input Output
DataInputStream
DataOutputStream
Class
Interface
FilterInputStream
DataInput
DataInputStream
Class
Interface
FilterOutputStream
DataOutput
DataOutputtStream
3
Program
mydata
fos
binary stream
mydata
dos
filter
fis
dis
binary stream
filter
Program
Screen
writeInt(120), writeInt(10120)
5
120
375.5
66
true
X
x@wx
}
}
file1+ file2
Buffer
Streams
Sequencer
inBuffer
read()
Program
write()
Buffer
Screen
outputBuffer
9
10
Example Program
import java.io.* ;
public class CombineStreams {
public static void main( String args[ ] ) throws I OException {
/ / declare file streams
FileI nputStream file1 = new FileI nputStream( "file1.dat") ;
FileI nputStream file2 = new FileI nputStream( "file2.dat") ;
/ / declare file3 to store combined streams
SequenceI nputStream file3 = null;
/ / concatenate file1 and file2 streams into file3
file3 = new SequenceI nputStream( file1, file2) ;
BufferedI nputStream inBuffer = new BufferedI nputStream( file3) ;
BufferedOutputStream outBuffer = new BufferedOutputStream( System.out) ;
/ / read and w rite combined streams until the end of buffers
int ch;
w hile( ( ch = inBuffer.read( ) ) != -1 )
outBuffer.w rite( ch) ;
outBuffer.flush( ) ; / / check out the output by removing this line
System.out.println( "\ nHello, This output is generated by CombineFiles.java program") ;
inBuffer.close( ) ;
outBuffer.close( ) ;
file1.close( ) ;
file2.close( ) ;
file3.close( ) ;
}
}
Hello,
I am C+ + , born in AT&T.
11
Hello,
I am Java, born in Sun Microsystems!
12
Output
Hello,
I am C+ + , born in AT&T.
Hello,
I am Java, born in Sun Microsystems!
Hello, This output is generated by CombineFiles.java program
13
0
Int
120
375.5
true
X
2003
File length: 23
4
Double
12
Int
boolean
Char
16
17
19
Int
23
15
16
Standard I/ O
14
Interactive IO Example
Create Tokenens
StringTokenizer st;
st = new StringTokenizer(str);
19
import java.io.* ;
import java.util.* ;
public class StudentRecord {
public static void main( String args[ ] ) throw s I OException {
/ / Create buffered reader for standard input
BufferedReader dis = new BufferedReader( new I nputStreamReader( System.in) ) ;
StringTokenizer st;
/ / reading data from console
System.out.print( "Enter Student I D: ") ;
st = new StringTokenizer( dis.readLine( ) ) ;
int stdI D = I nteger.parseI nt( st.nextToken( ) ) ;
System.out.print( "Enter Student Name: ") ;
String stdName = dis.readLine( ) ;
System.out.print( "Enter Student Marks: ") ;
st = new StringTokenizer( dis.readLine( ) ) ;
int stdMarks = I nteger.parseI nt( st.nextToken( ) ) ;
/ / w rite to console
System.out.println( "Student details are:") ;
System.out.println( "I D: "+ stdI D) ;
System.out.println( "Name: "+ stdName) ;
System.out.println( "Marks: "+ stdMarks) ;
}
}
20
Summary
21