11-io - COMP 202 File Access COMP 202 Introduction to...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - File Access 1 COMP 202 – File Access CONTENTS : I/O streams Reading and writing text files
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - File Access 2 I/O Streams • A stream is a sequence of bytes that flow from a source to a destination • In a program, we read information from an input stream and write information to an output stream • A program can manage multiple streams at a time • The java.io package contains many classes that allow us to define various streams with specific characteristics
Background image of page 2
COMP 202 – Introduction to Computing 1 COMP 202 - File Access 3 I/O Stream Categories • The classes in the I/O package divide input and output streams into other categories • An I/O stream is either a character stream , which deals with TexT data byte stream , which deal with byte ( b±NaRy ) data
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - File Access 4 Standard I/O • There are three standard I/O streams • The System class contains three object reference variables (in, out, err) – declared public and static (can be accessed via class name). • System.in – standard input (typically keyboard) – we give System.in as input to Scanner constructor to read from keyboard • System.out – standard output (typically a window on screen) println is method of out output stream, thus to print to standard output we call System.out.println
Background image of page 4
COMP 202 – Introduction to Computing 1 COMP 202 - File Access 5 The Standard Input Stream • We’ve used the standard input stream to create a Scanner object to process input read interactively from the user: Scanner scan = new Scanner (System.in); • The Scanner object converts bytes from the stream into characters, and provides various methods to access those characters (by line, by word, by type, etc.)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - File Access 6 Reading Text Files • We can read a text file sequentially using the file as the input stream for our scanner object: – Assume you have a file: test.txt – Alternative 1: • Construct a FileReader object and use that as input for the Scanner constructor • FileReader reader = new FileReader("test.txt"); • Scanner scan = new Scanner(reader); – Alternative 2: • Construct a File object and use that as input for the Scanner constructor • Scanner scan = new Scanner(new File("test.txt")); You can then use the Scanner operators ( next, nextLine, nextInt, …) to read the file sequentially.
Background image of page 6
COMP 202 – Introduction to Computing 1 COMP 202 - File Access
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 18

11-io - COMP 202 File Access COMP 202 Introduction to...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online