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.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
Files and Streams CS 21a Chapter 11 of Horstmann.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Lecture 7 Exceptions and I/O. Overview of the exception hierarchy A simplified diagram of the exception hierarchy in Java Throwable ErrorException IOException.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
Chapter 9 1 Chapter 9 – Part 1 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.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
1 Recitation 8. 2 Outline Goals of this recitation: 1.Learn about loading files 2.Learn about command line arguments 3.Review of Exceptions.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Introduction to Programming G50PRO University of Nottingham Unit 11 : Files Input/Ouput Paul Tennent
Java Chapter 9 File Input and Output. Objectives In this chapter you will: Learn how bits, characters, records, and files fit into a data hierarchy Explore.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Graphics in Java CS 21b. The paint() Method Method in a visual component that specifies what that component looks like Particularly important for applets,
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Files and Streams CS /02/05 L7: Files Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Java Programming Review (Part II) Enterprise Systems Programming.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
GENERICS AND FILE HANDLING Saumya Srivastava (977934) Divyangana Pandey (977790) Shubhi Saxena (978108) Arka Das (962969) AHD05/15-16 AJA 21.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Generics and file handling Presented by: Abha Kumari Neha Pradip Vhanmane Raj Visa Rashmi Kewlani Suvrat Dixit.
Java IO Exploring the java.io package and living to talk about it.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
OO Design and Programming II I/O: Reading and Writing
Streams & File Input/Output (I/O)
I/O Basics.
JAVA IO.
CSS 161: Fundamentals of Computing
File I/O ICS 111: Introduction to Computer Science I
Workshop for Programming And Systems Management Teachers
Workshop for Programming And Systems Management Teachers
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Web Design & Development Lecture 8
Exception Handling Contents
Streams and Readers The stream hierarchy is for reading bytes, the reader/writer hierarchy is for reading characters Unicode characters can be used internationally,
David Davenport Spring 2005
Presentation transcript:

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 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

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

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();

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

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

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

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 }

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., “

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

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

More Exercises on Files and Streams

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

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.

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.