Comp 212: Intermediate Programming Lecture 30 - PowerPoint PPT Presentation

About This Presentation
Title:

Comp 212: Intermediate Programming Lecture 30

Description:

Introduction to I/O Streams. Reader and Writer Classes. StreamTokenizer ... System.out.print(argv[j] ' '); System.out.println(); 14. Stdin, Stdout & Stderr ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 15
Provided by: Anupam66
Category:

less

Transcript and Presenter's Notes

Title: Comp 212: Intermediate Programming Lecture 30


1
Comp 212 Intermediate ProgrammingLecture 30
Stream and File I/O
  • By Anupam Chanda

2
Todays Menu
  • Introduction to I/O Streams
  • Reader and Writer Classes
  • StreamTokenizer and Parsing
  • Command-line Arguments
  • System Class

3
Stream I/O
  • Stream abstract concept of input and output
  • Sequence of data
  • Has a source or a destination
  • java.io package

4
Reading and Writing
READ
WRITE
  • open(stream)
  • while (more info)
  • read(stream)
  • close(stream)

open(stream) while (more info) write(stream) cl
ose(stream)
5
Types of Streams
  • Byte Streams
  • Operate on bytes (8-bit)
  • No further discussion
  • Character Streams
  • Operate on 16-bit characters
  • Reader and Writer

6
Reader and Writer
7
Reader
  • An abstract class
  • Can read from an abstract character source
  • Need a concrete subclass
  • Read from a concrete character source
  • FileReader fReader new FileReader(fileName)
  • fReader.read() next character from source

8
Writer
  • An abstract class
  • Can write to an abstract character destination
  • Need a concrete subclass
  • Write to a concrete character source
  • FileWriter fWriter new FileWriter(fileName)
  • fWriter.write() next character to destination

9
Read and Write Text Files
  • import java.io.
  • public class Copy
  • public static void main(String args) throws
    IOException
  • FileReader in new FileReader(in.txt)
  • FileWriter out new FileWriter(out.txt)
  • int c
  • while ((c in.read()) ! -1)
  • out.write(c)
  • in.close()
  • out.close()

10
(No Transcript)
11
(No Transcript)
12
StreamTokenizer and Parsing
  • Parse an input stream of characters
  • Identify words, numbers, etc.
  • Tokenizing breaking an input stream in tokens
  • int k 100
  • Tokens int, k, , 100,
  • java.io.StreamTokenizer
  • Example program on web page

13
Command-line arguments
  • Pass arguments to the program at run-time
  • public static void main(String argv)
  • for(int j 0 j lt argv.length j)
     System.out.print(argvj ") 
  • System.out.println()

14
Stdin, Stdout Stderr
  • System.out.println(Hello world!)
  • System.out PrintStream object
  • Writes to standard output (stdout) monitor
  • java MyClass gt out.txt
  • System.in stdin (keyboard)
  • java MyClass lt in.txt
  • System.err stderr (monitor)
  • java MyClass gt err.txt
  • PrintStream is deprecated
Write a Comment
User Comments (0)
About PowerShow.com