Presentation is loading. Please wait.

Presentation is loading. Please wait.

18 File i/o, Parsing.

Similar presentations


Presentation on theme: "18 File i/o, Parsing."— Presentation transcript:

1 18 File i/o, Parsing

2 Previously Defining Classes Inheritance of Properties
Inheritance of Methods Sub and super classes

3 Overview File & Streams Read Write Parsing

4 File i/o i/o refers to input and output
File i/o refers to the process of Reading file information Reading from a file Writing to a file Type of files are Directories Archives usually called files

5 File Types Two types of file: Text Binary
Files composed only of ASCII / Unicode characters Binary Files that may contain any type of data Archives - jar/zip files Programs - executable file Media - Images / Sound / Movie

6 Contents of a Binary File
Open with a text editor

7 File API File is a class Part of the java.io package File instances (objects) represent a file or directory in the system File instances (objects) do not provide access to the file content File class provides useful methods to retrieve information about a file or directory isDirectory(), isFile(), exists(), canRead(), canWrite(),… All i/o must be error trapped (i.e. a file can only be opened inside a try statement).

8 File Paths Relative Absolute Systems Separator
From where Program is running Absolute Full Path Systems POSIX /home/axmh03/public_html/test.txt Windows C:\Documents and Settings\axmh03\docs\test.txt Separator String separator = File.separator; String separator = FileSystems.getDefault().getSeparator();

9 File Constructors You can create a new empty directory / file object using the File constructor: File dir = new File(".\images"); File img1 = new File(dir, "logo.gif"); File img2 = new File(".\images\logo.gif"); File img3 = new File(".\images", "logo.gif"); File objects can be EITHER directories OR files

10 File Management Some useful methods
public boolean exists() true if the file exists and false otherwise public boolean createNewFile() throws IOException true if the new file was created (does not already exists) or false otherwise public boolean delete() throws SecurityException true if the file or directory is successfully deleted or false otherwise

11 Accessing File Contents
FileReader Assumes the default character encoding If different character encoding use class InputStreamReader FileWriter If different character encoding use class InputStreamWriter Buffered Streams: BufferedReader BufferedWriter

12 Parsing Text: Scanner We've already used to intercept keyboard input
Can also be used to read in a file and separate into tokens By default separated on whitespace Can change delimiter invoking useDelimiter() By default tokens are a String but there are methods to read in most basic types

13 Vectors Like an Elastic Array of Objects
Generally need to cast from generic type Useful for returning lists of things Java.util.Vector Vector<ObjectType> stuff = new Vector<ObjectType>();


Download ppt "18 File i/o, Parsing."

Similar presentations


Ads by Google