CMPT 270 Files and I/O Streams PDF

Document Details

NonViolentSerpentine2856

Uploaded by NonViolentSerpentine2856

University of Saskatchewan

Jason T Bowey

Tags

java programming file input/output streams programming

Summary

These lecture notes cover files and input/output streams in Java, including both text and binary formats. They detail how to use various streams such as FileInputStream and FileOutputStream, along with FileReader and FileWriter. The notes provide examples and explanations for reading from and writing to text files using the Scanner and PrintWriter classes.

Full Transcript

CMPT 270 Developing Object Oriented Systems Files and I/O Streams Jason T Bowey University of Saskatchewan Jason T Bowey CMPT 270 1/19 Learning Objectives ˆ Understand Java's Input and Output Steam objects ˆ Become familiar with Java Charac...

CMPT 270 Developing Object Oriented Systems Files and I/O Streams Jason T Bowey University of Saskatchewan Jason T Bowey CMPT 270 1/19 Learning Objectives ˆ Understand Java's Input and Output Steam objects ˆ Become familiar with Java Character and Byte stream classes for handling text and binary les ˆ Employ Java applications that interact with les and streams: ˆ Encrypting and decrypting data ˆ Reading a PNG le Jason T Bowey CMPT 270 2/19 Streams ˆ A Stream is an object that enables the ow of data between a program and some I/O device or le ˆ If the data ows into a program, then the stream is called an input stream ˆ If the data ows out of a program, then the stream is called an output stream ˆ Java stream objects are broadly categorized into two groups: ˆ Java Byte Streams - used to perform input and output for 8-bit bytes ˆ Java Character Streams - perform input and output for 16-bit unicode characters Jason T Bowey CMPT 270 3/19 Streams ˆ Input streams can ow from the keyboard or from a le ˆ System.in is an input stream that connects to the keyboard Scanner keyboard = new Scanner(System.in); ˆ Output streams can ow to a screen or to a le ˆ System.out is an output stream that connects to the screen System.out.println("Output stream"); Jason T Bowey CMPT 270 4/19 File ˆ Files are very commonly used to store infomation/data ˆ There are two ways to store data in a le: ˆ Text Format - data represented as a sequence of characters (Character streams) ˆ Binary Format - data represented as a sequence of bytes (Byte stream) Jason T Bowey CMPT 270 5/19 Text Files ˆ Designed to be read by human beings ˆ Computers use binary bit patterns to represent both numbers and characters ˆ Each character or symbol is represented with a unique number called a code point (e.g., the letter 'A' is mapped to 65) ˆ Dierent encoding techniques are adopted to cover a wide range of characters in various languages Jason T Bowey CMPT 270 6/19 Binary Files ˆ Consists of a sequence of binary digits and are designed to be read by programs ˆ An advantage of binary les is that they are more ecient to process than text les ˆ Unlike most binary les, Java binary les are platform independent Jason T Bowey CMPT 270 7/19 Data Representation in les Jason T Bowey CMPT 270 8/19 Java Classes for Handling Text Files Jason T Bowey CMPT 270 9/19 Java Classes for Handling Binary Files Jason T Bowey CMPT 270 10/19 Why Two Sets of Classes? ˆ FileInputStream and FileOutputStream used Java Byte Streams ˆ FileReader and FileWriter classes use Java Character Streams ˆ Though internally, FileReader uses FileInputStream and FileWriter uses FileOutputStream Jason T Bowey CMPT 270 11/19 Reading from a Text File Using Scanner ˆ The Scanner class can be used for reading from the keyboard as well as from a text le ˆ Simple replace System.in in the Scanner's constructor with a suitable steam connected to the le   1 Scanner stream = new Scanner ( 2 new FileInputStream ( filename ));   ˆ Methods of the Scanner class for reading input behave the same whether reading from the keyboard or reading from a text le ˆ e.g., the nextInt method Jason T Bowey CMPT 270 12/19 Text File Input 1. Create an object of the File class   1 File inputFile = new File ( " input. txt " );   2. Create an object of the Scanner class   1 Scanner in = new Scanner ( inputFile );   3. Use the regular Scanner methods   1 while ( in. hasNextLine ()) 2 { 3 String line = in. nextLine (); 4 // process line of text 5 }   Jason T Bowey CMPT 270 13/19 Writing to a Text File The class PrintWriter is a stream class that can be used to write to a text le ˆ An object of the class PrintWriter has the methods print, printf, and println ˆ These are similar to the System.out methods of the same names, but used for text le output instead of screen output ˆ Converts characters into a number following an encoding technique and then translate the number into bits Jason T Bowey CMPT 270 14/19 Text File Output 1. Create an object of the PrintWriter class   1 PrintWriter out = new PrintWriter (" output. txt " )   ˆ If output.txt exists, it will be emptied ˆ If output.txt doesn't exist, it will create an empty le 2. Use PrintWriter methods   1 out. println (" Hello , world !" );   Jason T Bowey CMPT 270 15/19 Closing Files ˆ You must use the close method before le reading and writing is complete - your text may not be saved to the le until you use the close method! ˆ Closing a Scanner  1 while ( in. hasNextLine ()) 2 { 3 String line = in. nextLine (); 4 } 5 in. close ();   ˆ Closing a PrintWriter  1 out. println (" Hello , world !" ); 2 out. close ();   Jason T Bowey CMPT 270 16/19 Closing Files To read binary data from a disk le, you create a FileInputStream object   1 InputStream inputStream = new 2 FileInputStream (" input. bin " );   Use FileOutputStream to write data to disk le in a binary form   1 OutputStream outputStream = new 2 FileOutputStream ( " output. bin " );   Jason T Bowey CMPT 270 17/19 Exercise 1: Reading and Writing to a Text File ˆ Write a program that reads the contents of a text le ands each line of text to a List ˆ Print out the text to the console ˆ Write the text into a new le, in reverse order Jason T Bowey CMPT 270 18/19 Summary ˆ IO Streams can use Characters or Bytes ˆ Use FileInputStream and FileOutputStream for Byte Streams ˆ Use FileReader and FileWriter for Character Streams Jason T Bowey CMPT 270 19/19

Use Quizgecko on...
Browser
Browser