fileio - CS 134 Principles of Computer Science Winter 2008...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CS 134 Principles of Computer Science Winter 2008 Java Review Session File I/O Presented by CS 134 Tutors 1 1 Motivation Why do we want to read and write to files? Advantage: Can save (and subsequently load) information about the program state and data in a permanent way. For example, saving (and loading) a users progress in a game. Advantage: Allows us to provide input to programs using other means than the keyboard. This is especially advantageous when input rarely changes, is large, or already exists. For example, we can design a program to read a list of student grades and compute an average. We are provided with a spreadsheet (e.g. an Excel file) with a list of student numbers and grades. 2 2 The File Class Java SDK Version 1.5 provides a class which represents a file on the disk. This class is appropri- ately named File . When a File object is instantiated, the programmer typically provides a filename (including an absolute or relative path) to the constructor. To create a new File object in this manner, we use the following syntax: File myFile = new File(pathName); where myFile is the name of the File object we wish to create, and pathName is a String containing the path to the file we wish to open. Here is an example of File instantiation. File friends = new File("friends.txt"); Once we have instantiated a File object, we can read and write its data by using methods provided in other classes. We will see this in the following sections. Additionally, file-related attributes (e.g. read only, hidden) can be set using methods in the File class, and there are also methods for file system operations such as renaming and deleting files. You may occasionally find these methods useful. Here is an abridged class diagram for File : File . . . + File(String pathname) + boolean delete() + String getName() + boolean renameTo(File dest) + boolean setReadOnly() . . . For complete reference and documentation of the File class, see the Java 1.5 API at: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html 3 3 Reading From a File the Scanner Class Java SDK version 1.5 provides a class which tokenizes input. Tokenizing essentially means split- ting input into chunks (called tokens ); these tokens are usually separated by whitespace (spaces, tabs \t and new lines \n ). This class is called Scanner . The following is an abridged class diagram of class Scanner . Complete documentation can be found at http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html Scanner (no instance variables) + Scanner(String source) + Scanner(File source) + void close() + String next() + String nextLine() + int nextInt() + double nextDouble() + boolean nextBoolean() + boolean hasNext()...
View Full Document

This note was uploaded on 01/25/2010 for the course CS CS134 taught by Professor Cl during the Fall '07 term at Waterloo.

Page1 / 10

fileio - CS 134 Principles of Computer Science Winter 2008...

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

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