COM214J2: Introduction to Programming Week 5 File IO - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

COM214J2: Introduction to Programming Week 5 File IO

Description:

public int read() throws IOException. Read a single character. Returns: ... System.out.println(line); catch (IOException exception) exception.printStackTrace ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 15
Provided by: sarabjots
Category:

less

Transcript and Presenter's Notes

Title: COM214J2: Introduction to Programming Week 5 File IO


1
COM214J2 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

2
I/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)
4
The 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

5
Byte Stream Constructors
  • FileInputStream
  • FileOutputStream

6
Byte Streams Usage
  • FileInputStream
  • FileOutputStream

7
import 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())
8
Filtering 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

9
Declaring 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
10
Character 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

11
Character Stream Constructors
  • FileReader
  • FileWriter

12
Character 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

13
Character Stream Filters
  • Buffered Reader/Buffered Writer

FileReader file new FileReader(myFile.txt) B
ufferedReader buffFile new BufferedReader(file)
  String Line BuffFile.readLine()
14
import 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()
Write a Comment
User Comments (0)
About PowerShow.com