Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.

Similar presentations


Presentation on theme: "Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O."— Presentation transcript:

1 Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O

2 Chapter 9 2 *Special Note: Using Path Names l Path name— filename and the full directory to the file l Relative path name— path below the directory containing the running program l UNIX/Linux path name: /user/smith/home.work/java/FileClassDemo.java l Windows path name: D:\Work\Java\Programs\FileClassDemo.java l to use a backslash ( \ ) in a quoted string, it is preceeded by another backslash "D:\\Work\\Java\\Programs\\FileClassDemo.java" l Java accepts path names in UNIX/Linux or Windows format, regardless of current OS

3 Chapter 9 3 Reading Words in a String: Using StringTokenizer Class there are BufferedReader methods to read a line string or a single character, but not just a single word the StringTokenizer class can be used to parse a line into individual words »load the library: java.util.* »its useful methods (page 564-565) –.countTokens(),.nextToken(),.hasMoreTokens() »two constructors exist that define how the input string is delimited; delimiter-the character used for separation –default delimiter is "white space" (space, tab, newline) »tokenizing a string can be used in many situations

4 Chapter 9 4 Example: StringTokenizer l display the words separated by the following chars: space, newline ( \n ), period (. ), or comma (, ). String inputLine = SavitchIn.readLine(); // wordFinder will hold the tokenized list StringTokenizer wordFinder = new StringTokenizer( inputLine, " \n.," ); while ( wordFinder.hasMoreTokens() ) { System.out.println( wordFinder.nextToken() ); } Question 2b or !tooBee input: " Question,2b.or !tooBee. " output:

5 Chapter 9 5 Testing for "End of File" in a Text File l text files end with a special character: EOF l during the reading of a text file, Java can detect if the EOF character has been read EOF = ASCII value 26 (end of file) CR = ASCII value 13 (carriage return) – '\r' NL = ASCII value 10 (linefeed, newline) – '\n' Tab = ASCII value 9 – '\t' if. readLine() attempts to read beyond the EOF char, it returns a null value (test for this to stop) if. read() returns an Integer value -1; the Unicode value -1, is special since all characters are positive l IMPORTANT: »neither of these methods throws an EOFException. how is this possible ?

6 Chapter 9 6 Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Excerpt from TextEOFDemo Example: Using Null to Test for End-of-File in a Text File When using readLine() test for null When using read() test for -1

7 Chapter 9 7 Examining class SavitchIn : readChar() Method public static char readChar() { int charAsInt = -1; try { charAsInt = System.in.read(); } catch (IOException e) { System.out.println(e.getMessage()); System.out.println("IO error. End."); System.exit(0); } return (char)charAsInt; }// end of readChar() Returns an int, not a char Initialized to avoid compiler error message.

8 Chapter 9 8 Examining Class SavitchIn : readLine() Method public static String readLine() { char nextChar; String result = ""; boolean done = false; while (!done) { nextChar = readChar(); if (nextChar == '\n') done = true; else if (nextChar == '\r') { } else result = result + nextChar; } return result; }// end of readLine() Do nothing. Next iteration will detect '\n' '\r' is carriage return symbol. some systems use "\r\n" to indicate "end of a line." Add any char except '\r' and '\n' to result string.

9 Chapter 9 9 In place of a String filename: the File class l acts like a wrapper class for file names file name, " numbers.dat " is only a String a file name of type File, has special methods ».exists() : return boolean, if file already exists ».canRead() : return boolean, if file can be read »see page 570-573 FileInputStream, FileReader, and FileOutputStream have constructors that take a File argument »similar to the constructors that take String arguments


Download ppt "Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O."

Similar presentations


Ads by Google