Title: COM214J2: Introduction to Programming Week 5 File IO
1COM214J2 Introduction to ProgrammingWeek 5File
I/O
- Sarabjot Singh Anand
- 16E17
- ss.anand_at_ulster.ac.uk
- http//www.infc.ulst.ac.uk/cgi-bin/infdb/homePage?
emailss.anand_at_ulster.ac.uk
2I/O Streams
- Model interactions between the program and data
- Implemented in the java.io package
- Allow the programmer to
- Create Input Streams to read data form any data
source - Create Output Stream to write data to any storage
media - Stating the Obvious To use these Streams you
must import the java.io package (import
java.io.) or specific classes (import
java.io.FileInputStream)
3(No Transcript)
4The java.io Package
- Consists of a number of classes representing
input and output streams - Byte Streams handles bytes, integers and other
simple data types - FileInputStream
- FileOutputStream
- Character Streams handle text files and other
text sources - Object Streams handle input/output of Objects
5Byte Stream Constructors
- FileInputStream
- FileOutputStream
6Byte Streams Usage
- FileInputStream
- FileOutputStream
7import java.io. public class ReadBytes
public static void main(String
arguments) try FileInputStream file
new FileInputStream(class.dat) FileOutputStr
eam outFile new FileInputStream(outClass.dat)
boolean eof false int count
0 while (!eof) int input
file.read() System.out.print(input
) outFile.write(input) if (input
-1) eof true else count
file.close() outFile.close() System.out.p
rintln(\nBytes read count) catch
(IOException e) System.out.println(Error ..
e.toString())
8Filtering a Stream
- A filter modifies the way an existing stream is
handled - BufferedInputStream/ BufferedOutputStream
- Rather than writing each character to the disk,
it buffers the data and occasionally writes to
disk when the buffer is full or when the stream
in flushed - DataInputStream/ DataOutputStream
- readBoolean(), readByte(),readLong(),
readShort(), readInt(), readFloat(), readDouble() - Corresponding write() methods
9Declaring Streams with Filters
FileInputStream file new FileInputStream(class.
dat) BufferedInputStream buffer new
BufferedInputStream(file) DataInputStream
dataBuffer new DataInputStream(buffer) int i
dataBuffer.readInt()
Buffered Input Stream
File Input Stream
Data Input Stream
Java Program
Data Source
10Character Streams
- Used to work with any text that is represented by
the ASCII or Unicode character sets - Should be used in all text input output rather
than using the byte streams - Subclasses of Reader and Writer classes in
java.io - Examples FileReader and FileWriter
11Character Stream Constructors
12Character Stream Usage
- public int read() throws IOException
- Read a single character
- Returns
- The character read, or -1 if the end of the
stream has been reached - public int read(char cbuf, int offset,
int length) throws IOException - Read characters into a portion of an array
- Parameters
- cbuf - Destination buffer
- offset - Offset at which to start storing
characters - length - Maximum number of characters to read
- Returns
- The number of characters read, or -1 if the end
of the stream has been reached
13Character Stream Filters
- Buffered Reader/Buffered Writer
FileReader file new FileReader(myFile.txt) B
ufferedReader buffFile new BufferedReader(file)
String Line BuffFile.readLine()
14import java.io. public class fileio
public static void main (String args)
String line String file "C\\Sarab\\teaching
\\students\\COM214J2 \\Assignment\\2004\\data.txt
" try FileReader fr new FileReader
(file) BufferedReader inFile new
BufferedReader (fr) while ((line
inFile.readLine()) ! null) System.out.println(
line) catch (IOException exception) exc
eption.printStackTrace()