chapter 9 1 chapter 9 – part 2 l overview of streams and file i/o l text file i/o l binary file...
TRANSCRIPT
![Page 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/1.jpg)
Chapter 9 1
Chapter 9 – Part 2
Overview of Streams and File I/O Text File I/O Binary File I/O File Objects and File Names
Streams and File I/O
![Page 2: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/2.jpg)
Chapter 9 2
*Special Note: Using Path Names
Path name—filename and the full directory to the file Relative path name—path below the directory
containing the running program UNIX/Linux path name:/user/smith/home.work/java/FileClassDemo.java Windows path name:D:\Work\Java\Programs\FileClassDemo.java to use a backslash ( \ ) in a quoted string, it is preceeded
by another backslash"D:\\Work\\Java\\Programs\\FileClassDemo.java" Java accepts path names in UNIX/Linux or Windows
format, regardless of current OS
![Page 3: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/3.jpg)
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
![Page 4: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/4.jpg)
Chapter 9 4
Example: StringTokenizer
display the words separated by the following chars: space, newline ( \n ), period ( . ), or comma ( , ).
String inputLine = SavitchIn.readLine();
// wordFinder will hold the tokenized listStringTokenizer wordFinder = new StringTokenizer( inputLine, " \n.," );
while ( wordFinder.hasMoreTokens() ){ System.out.println( wordFinder.nextToken() );}
Question2bor!tooBee
input: "Question,2b.or !tooBee." output:
![Page 5: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/5.jpg)
Chapter 9 5
Testing for "End of File" in a Text File
text files end with a special character: EOF during the reading of a text file, Java can detect if the EOF
character has been readEOF = 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
IMPORTANT: » neither of these methods throws an EOFException. how is
this possible ?
![Page 6: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/6.jpg)
Chapter 9 6
int count = 0; String line = inputStream.readLine(); while (line != null) { count++; outputStream.println(count + " " + line); line = inputStream.readLine(); }
Chapter 9 Java: an Introduction to Computer Science & Programming - Walter Savitch 6
Excerpt from TextEOFDemo
Example: Using Null toTest for End-of-File in a Text File
When using readLine()test for null
When using read() test for -1
![Page 7: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/7.jpg)
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.
![Page 8: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/8.jpg)
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.
![Page 9: 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](https://reader036.vdocuments.us/reader036/viewer/2022082713/5697bfc21a28abf838ca515c/html5/thumbnails/9.jpg)
Chapter 9 9
In place of a String filename: the File class
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