Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files and Streams CS 105. 10/02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.

Similar presentations


Presentation on theme: "Files and Streams CS 105. 10/02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved."— Presentation transcript:

1 Files and Streams CS 105

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

3 10/02/05 L7: Files Slide 3 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Input/Output Classes in Java I/O viewed as a stream of bytes parent classes: InputStream, OutputStream As a stream of (Unicode) characters parent classes: Reader, Writer 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 InputStream, OutputStream, Reader, or Writer

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

5 10/02/05 L7: Files Slide 5 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Text Files, continued To read from text files, use either DataInputStream or BufferedReader f = new DataInputStream( new FileInputStream( “filename.txt” ) ); f = new BufferedReader( new FileReader( “filename.txt” ) ); Use read methods to read from file s = f.readLine(); // reads a string

6 10/02/05 L7: Files Slide 6 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Exceptions File operations throw exceptions Make sure statements are enclosed in a try-catch statement 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 use a try-catch chain to distinguish different exceptions

7 10/02/05 L7: Files Slide 7 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Try-catch Chain try { … file operations … } catch( FileNotFoundException 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 … } … You can use a “try-catch chain” to catch specific exceptions AND / OR, you can catch IOException to catch any kind of IOException

8 10/02/05 L7: Files Slide 8 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved java.io.* Summary There is a host of classes under this package that serve a variety of purposes Hints: use “javap java.io.classname” to find out available constructors and methods you often need to use FileInputStream, FileOutputStream, FileReader, and FileWriter to associate a name to the file

9 10/02/05 L7: Files Slide 9 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Reading structured text 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: Open the file as a BufferedReader Call readLine() inside a while loop—use ready() to check if there is succeeding input Parse the string returned by readLine() using StringTokenizer apples,3,5.00 oranges,2,7.50 grapes,6,8.25

10 StringTokenizer

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

12 10/02/05 L7: Files Slide 12 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved StringTokenizer (cont’d.) Selected Methods int countTokens() String nextToken() boolean hasMoreTokens() To use this, you will have to import java.util.*;

13 10/02/05 L7: Files Slide 13 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved StringTokenizer Example StringTokenizer st; String longstr = “This is the last slide”; st = new StringTokenizer(longstr, “ ”); int numwords = st.countTokens(); System.out.println(numwords); String firstword = st.nextToken(); System.out.println(firstword); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }


Download ppt "Files and Streams CS 105. 10/02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved."

Similar presentations


Ads by Google