{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info iconThis preview shows pages 1–7. 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 Document Right 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 ( binary ) data
Background image of page 3

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

View Full Document Right 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 Document Right 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
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}