Files and Streams CS 105 100205 File n

  • Slides: 13
Download presentation
Files and Streams CS 105 10/02/05

Files and Streams CS 105 10/02/05

File n Unit of “secondary” storage n n Stores a sequence of bytes/characters n

File n Unit of “secondary” storage n n Stores a sequence of bytes/characters n n 10/02/05 as opposed to “primary” storage in memory Stream operations: read from stream, write to stream Associated with a filename Often organized under a directory hierarchy Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

Input/Output Classes in Java n n I/O viewed as a stream of bytes n

Input/Output Classes in Java n n I/O viewed as a stream of bytes n parent classes: Input. Stream, Output. Stream n parent classes: Reader, Writer As a stream of (Unicode) characters Need to import java. io. *; Note: applets can’t read or write files, only applications can * An application employing files will use a subclass of Input. Stream, Output. Stream, Reader, or Writer 10/02/05 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

Text Files n To create a text file, use Print. Stream n n To

Text Files n To create a text file, use Print. Stream n n To write to the text file use print methods n n 10/02/05 f. println(…); // use like System. out Make sure to close the file before exiting the program n n f = new Print. Stream( new File. Output. Stream( “filename. txt” ) ); f. close(); // ensures contents are updated If you want to update the file without closing it yet, you can call f. flush(); Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

Text Files, continued n To read from text files, use either Data. Input. Stream

Text Files, continued n To read from text files, use either Data. Input. Stream or Buffered. Reader n n n Use read methods to read from file n 10/02/05 f = new Data. Input. Stream( new File. Input. Stream( “filename. txt” ) ); f = new Buffered. Reader( new File. Reader( “filename. txt” ) ); s = f. read. Line(); // reads a string Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

Exceptions n n File operations throw exceptions Make sure statements are enclosed in a

Exceptions n n File operations throw exceptions Make sure statements are enclosed in a try-catch statement n n if you look at Java docs, you will see that the file I/O methods say “throws IOException” this means that the compiler will require you to catch IOException n 10/02/05 use a try-catch chain to distinguish different exceptions Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

Try-catch Chain n n 10/02/05 You can use a “try-catch chain” to catch specific

Try-catch Chain n n 10/02/05 You can use a “try-catch chain” to catch specific exceptions AND / OR, you can catch IOException to catch any kind of IOException try { … file operations … } catch( File. Not. Found. Exception se ) { … if file is not found … } catch( EOFException ee ) { … if no more data to read … } catch( IOException e ) { … for all other cases not yet covered … } … Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

java. io. * Summary n n There is a host of classes under this

java. io. * Summary n n There is a host of classes under this package that serve a variety of purposes Hints: n n 10/02/05 use “javap java. io. classname” to find out available constructors and methods you often need to use File. Input. Stream, File. Output. Stream, File. Reader, and File. Writer to associate a name to the file Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

Reading structured text n n Often, an application would read text data from a

Reading structured text n n Often, an application would read text data from a file one line at a time, where each line contains data separated by special characters What to do: n n n 10/02/05 apples, 3, 5. 00 oranges, 2, 7. 50 grapes, 6, 8. 25 Open the file as a Buffered. Reader Call read. Line() inside a while loop—use ready() to check if there is succeeding input Parse the string returned by read. Line() using String. Tokenizer Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

String. Tokenizer 10/02/05

String. Tokenizer 10/02/05

String. Tokenizer n n Breaks up a string into substrings (tokens) separated by a

String. Tokenizer n n Breaks up a string into substrings (tokens) separated by a specified delimiter Provides 3 constructors: n n n 10/02/05 String. Tokenizer(String s, String delim, boolean return. Tokens) String. Tokenizer(String s, String delim) String. Tokenizer(String s) Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

String. Tokenizer (cont’d. ) n Selected Methods n n 10/02/05 int count. Tokens() String

String. Tokenizer (cont’d. ) n Selected Methods n n 10/02/05 int count. Tokens() String next. Token() boolean has. More. Tokens() To use this, you will have to import java. util. *; Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05

String. Tokenizer Example String. Tokenizer st; String longstr = “This is the last slide”;

String. Tokenizer Example String. Tokenizer st; String longstr = “This is the last slide”; st = new String. Tokenizer(longstr, “ ”); int numwords = st. count. Tokens(); System. out. println(numwords); String firstword = st. next. Token(); System. out. println(firstword); while (st. has. More. Tokens()) { System. out. println(st. next. Token()); } 10/02/05 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L 7: Files Slide 10/02/05