Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files and Streams CS 21a. 10/02/05 L18: 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 21a. 10/02/05 L18: 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 21a

2 10/02/05 L18: 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 L18: 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 L18: 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 L18: 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 L18: 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 Or, add throws IOException to the declaration of the method that uses files

7 10/02/05 L18: 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 L18: Files Slide 8 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Throwing exceptions If you choose not to catch exceptions, you must declare that they will be thrown This means when a file-related exception does occur, a run-time error will result public static void main( String args[] ) throws IOException { … file operations not enclosed in a try-catch statement }

9 10/02/05 L18: Files Slide 9 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Reading a File from the Web Use URL class from java.net To open: wpage = new URL( address ); f = new BufferedReader( new InputStreamReader( wpage.openStream() ) ); address is a String specifying the webpage address (e.g., “http://www.admu.edu.ph”)

10 10/02/05 L18: Files Slide 10 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

11 10/02/05 L18: Files Slide 11 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Exercises PrintStream and TextFiles Do a “Hello World” program that writes to a text file instead of the screen Write a “Type” program that prints out the contents of any text file (given as a command-line parameter) to the screen

12 More Exercises on Files and Streams

13 10/02/05 L18: Files Slide 13 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Part1: The Basics 1. Write a program that prints your favorite poem to a file called "poem.txt" in the current directory. 2. Write a program that prints the lyrics of the 12 days of Christmas to "12days.txt". Use a loop so that you don't actually have to write a gazillion print statements. 3. Write a program that reads a line from the user (remember JOptionPane.showInputDialog?) to a text file called "chat.txt". The program should not overwrite the text file each time you call it, but only add it to the end. Hint: search on the Internet or check out the JDK documentation for FileWriter

14 10/02/05 L18: Files Slide 14 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Part2: String Manipulation 1. Find out what happens when you try to read a file that does not exist. 2. Find out what happens when you try to read more lines from a file than it actually has. 3. If you don't have a file called "poem.txt" yet, use Notepad to create it. Make sure it has at least three lines. Write a program that displays the first three lines of "poem.txt". 4. If you don't have a file called "poem.txt" yet, use Notepad to create it. Make sure it has at least three lines. Write a program that displays the first three lines of "poem.txt" in reverse order - the third line, then the second line, then the first. 5. Write a program that prints out the first line of a file in reverse that is, abcd should become dcba.

15 10/02/05 L18: Files Slide 15 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved Part 3: Reading all lines from a File 1. Print out all the lines in "song.txt" as is. 2. Print out all the lines in "song.txt", but convert them to lowercase letters. 3. Print out all the lines in "song.txt", but in reverse order. The last line, should be printed first, and so on. 4. Print out every other line of "song.txt". 5. Given several filenames as arguments to your Java program (remember the args[] array?), print out all of their contents.


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

Similar presentations


Ads by Google