Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 16 – Files and Streams. Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections.

Similar presentations


Presentation on theme: "Chapter 16 – Files and Streams. Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections."— Presentation transcript:

1 Chapter 16 – Files and Streams

2 Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections “encouraged” Responsible for online supplements for Exceptions and File I/O (see syllabus) Responsible for online supplements for Exceptions and File I/O (see syllabus)

3 Chapter Goals To be able to read and write text files To be able to read and write text files To become familiar with the concepts of text and binary formats To become familiar with the concepts of text and binary formats To learn about encryption To learn about encryption To understand when to use sequential and random file access To understand when to use sequential and random file access

4 MEM CPU HDD keyboard monitor terminal console standard input stream standard output stream Streams What does information travel across?

5 MEM CPU HDD keyboard monitor terminal console standard input stream standard output stream file input stream LOAD READ file output stream SAVE WRITE Streams files What does information travel across?

6 16.1 Reading and Writing Text Files Text files – files containing plain text Text files – files containing plain text Created with editors such as notepad, etc. Created with editors such as notepad, etc. Simplest way to learn it so extend our use of Scanner Simplest way to learn it so extend our use of Scanner Associate with files instead of System.in Associate with files instead of System.in All input classes, except Scanner, are in java.io All input classes, except Scanner, are in java.io import java.io.*; import java.io.*;

7 Review: Scanner Two ways to use scanner  two constructors Two ways to use scanner  two constructors First constructors takes an object of type java.io.InputStream – stores information about the connection between an input device and the computer or program First constructors takes an object of type java.io.InputStream – stores information about the connection between an input device and the computer or program Example: System.in Example: System.in Recall – only associate one instance of Scanner with System.in in your program Recall – only associate one instance of Scanner with System.in in your program

8 Review: Numerical Input First way: First way: Use nextInt() Use nextInt() int number = scanner.nextInt(); Second way: Second way: Use nextLine(), Integer.parseInt() Use nextLine(), Integer.parseInt() String input = scanner.nextLine(); int number = Integer.parseInt(input);

9 What’s the difference? Exceptions Exceptions nextInt() throws InputMismatchException nextInt() throws InputMismatchException parseInt() throws NumberFormatException parseInt() throws NumberFormatException Optimal use Optimal use nextInt() when multiple information on one line nextInt() when multiple information on one line nextLine() + parseInt() when one number per line nextLine() + parseInt() when one number per line

10 Reading To read from a disk file, construct a FileReader To read from a disk file, construct a FileReader Then, use the FileReader to construct a Scanner object Then, use the FileReader to construct a Scanner object FileReader reader = new FileReader("input.txt"); Scanner in = new Scanner(reader);

11 Alternative Use File instead of FileReader Use File instead of FileReader Has an exists( ) method we can call to avoid FileNotFoundException Has an exists( ) method we can call to avoid FileNotFoundException File file = new File ("input.txt"); Scanner in; if(file.exists()){ in = new Scanner(file); } else { //ask for another file }

12 What does this do? Allows us to use methods we already know Allows us to use methods we already know next, nextLine, nextInt, etc. next, nextLine, nextInt, etc. Reads the information from the file instead of console Reads the information from the file instead of console

13 File Class java.io.File java.io.File associated with actual file on hard drive associated with actual file on hard drive used to check file's status used to check file's status Constructors Constructors File( ), File(, ) File( ), File(, ) Predicate Methods Predicate Methods exists() exists() canRead(), canWrite() canRead(), canWrite() isFile(), isDirectory() isFile(), isDirectory()

14 Writing To File We will use a PrintWriter object to write to a file We will use a PrintWriter object to write to a file What if file already exists?  Empty file (delete whatever is there) What if file already exists?  Empty file (delete whatever is there) Doesn’t exist?  Create empty file with that name Doesn’t exist?  Create empty file with that name How do we use a PrintWriter object? How do we use a PrintWriter object? Have we already seen one? Almost. Have we already seen one? Almost.

15 PrintWriter The out field of System is a PrintStream object associated with the console. PrintWriter is a similar class optimized for writing characters. The out field of System is a PrintStream object associated with the console. PrintWriter is a similar class optimized for writing characters. We will associate our PrintWriter with a file now We will associate our PrintWriter with a file now Can use either a filename or File object Can use either a filename or File object PrintWriter fileOut = new PrintWriter("output.txt"); fileOut.println(29.95); fileOut.println(new Rectangle(5, 10, 15, 25)); fileOut.println("Hello, World!"); This will print the exact same information as with System.out (except to a file “output.txt”)! This will print the exact same information as with System.out (except to a file “output.txt”)!

16 Closing File Only difference is that we have to close the file stream when we are done writing Only difference is that we have to close the file stream when we are done writing If we do not, some output may not get written If we do not, some output may not get written At the end of output, call close() At the end of output, call close()fileOut.close();

17 Why? Short answer Short answer When you call print( ) and/or println( ), the output is actually written to buffer. When you close or flush the output, the buffer is written to the file When you call print( ) and/or println( ), the output is actually written to buffer. When you close or flush the output, the buffer is written to the file The slowest part of the computer is hard drive operations – much more efficient to write once instead of writing repeated times The slowest part of the computer is hard drive operations – much more efficient to write once instead of writing repeated times

18 File name When determining a file name, default is to place in the same directory as your.class files When determining a file name, default is to place in the same directory as your.class files If we want to define other place, use absolute path (e.g. C:\My Documents) If we want to define other place, use absolute path (e.g. C:\My Documents) in = new FileReader(“C:\\homework\\input.dat”);

19 Getting it all to work Remember: Remember: Have to import from java.io Have to import from java.io I/O requires us to catch checked exceptions I/O requires us to catch checked exceptions java.io.IOException java.io.IOException How long do we read from the file? Until the end. (duh) Use the hasNext( ), hasNextLine( ) and hasNextInt( ) predicate methods from Scanner. Otherwise you risk creating a NoSuchElementException

20 Java Input Review CONSOLE: Scanner stdin = new Scanner( System.in ); FILE: Scanner inFile = new Scanner( new File ( srcFileName ) );

21 import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; public class LineNumberer { public static void main(String[] args){ Scanner console = new Scanner(System.in); System.out.print(“Enter input file: "); String inFile = console.next(); System.out.print(“Enter output file: "); String outFile = console.next();

22 try{ File reader = new File(inFile); Scanner in = new Scanner(reader); PrintWriter out = new PrintWriter(outputFileName); int lineNumber = 1; while (in.hasNextLine()){ String line = in.nextLine(); out.println("/* " + lineNumber + " */ " + line); lineNumber++;}out.close(); } catch (IOException exception){ System.out.println("Error processing file: " + exception.getMessage()); System.out.println("Error processing file: " + exception.getMessage());} }}

23 Common Error You can run into problems using nextLine( ) in conjunction with nextInt( ) from the Scanner class. You can run into problems using nextLine( ) in conjunction with nextInt( ) from the Scanner class. In order to read this file In order to read this file You typed this code, but got this output You typed this code, but got this output What went wrong? What went wrong? 77hello int I = input.nextInt(); String s = input.nextLine(); System.out.println(i+”,”+s); 77,

24 Buffering gone bad To Java, the file is a long buffer of characters To Java, the file is a long buffer of characters nextInt removes the characters corresponding to a number, and that’s all. nextInt removes the characters corresponding to a number, and that’s all. nextLine looks for the next newline character (‘\n’), and returns everything before the first one it finds, even if that String is empty! nextLine looks for the next newline character (‘\n’), and returns everything before the first one it finds, even if that String is empty! 77\nhell… \nhello\n… hello\n… i = 77 s = “”

25 What to do? Avoid using nextInt( ) and nextLine( ) in combination Avoid using nextInt( ) and nextLine( ) in combination Always use nextLine( ) and convert to integers using Integer.parseInt( ) Always use nextLine( ) and convert to integers using Integer.parseInt( ) Use nextInt( ) in conjunction with next( ), which will skip over newlines to find the next non-whitespace string Use nextInt( ) in conjunction with next( ), which will skip over newlines to find the next non-whitespace string Check to see if Strings from nextLine( ) have length 0, and if so, call it again. Check to see if Strings from nextLine( ) have length 0, and if so, call it again.

26 16.3 An Encryption Program Demonstration: Use encryption to show file techniques Demonstration: Use encryption to show file techniques File encryption File encryption To scramble a file so that it is readable only to those who know the encryption method and secret keyword To scramble a file so that it is readable only to those who know the encryption method and secret keyword (Big area of CS in terms of commercial applications – biometrics, e-commerce, etc.) (Big area of CS in terms of commercial applications – biometrics, e-commerce, etc.)

27 Caesar Cipher Encryption key – the function to change the value Encryption key – the function to change the value Simple key – shift each letter over by 1 to 25 characters Simple key – shift each letter over by 1 to 25 characters If key = 3, A  D B  E etc. If key = 3, A  D B  E etc. Decrypt = reverse the encryption Decrypt = reverse the encryption Here we just subtract the key value Here we just subtract the key value

28 Caesar Cipher for alphabetic characters public void encrypt (Scanner in, PrintWriter out, int key) { while (in.hasNextLine()) { String line = in.nextLine(); String outLine = “”; for (int i=0; i<line.length; i++) { char c = line.charAt(i); if (c >= ‘a’ && c = ‘a’ && c <= ‘z’) c = (char)((c – ‘a’ + key) % 26 + ‘a’); else if (c >= ‘A’ && c = ‘A’ && c <= ‘Z’) c = (char)((c – ‘A’ + key) % 26 + ‘A’); outLine += c; }out.println(outLine);}} "Meet me at the secret place." key=5 => "Rjjy rj fy ymj xjhwjy uqfhj."

29 Modifications of Output Two constraints so far: Two constraints so far: Files are overwritten Files are overwritten Output is buffered and not written immediately Output is buffered and not written immediately We have options to get around this if we need to We have options to get around this if we need to More on that after this… More on that after this…

30 Tokenizing Often several text values are in a single line in a file to be compact Often several text values are in a single line in a file to be compact “25 38 36 34 29 60 59” Line must be broken into parts (i.e. tokens) Line must be broken into parts (i.e. tokens)“25”“38”“36” Tokens then can be parsed as needed Tokens then can be parsed as needed “25” can be turned into the integer 25

31 Why Inputting each value on a new line makes the file very long Inputting each value on a new line makes the file very long May want a file of customer info – name, age, phone number all on one line May want a file of customer info – name, age, phone number all on one line File usually separate each piece of info with a delimiter – any special character designating a new piece of data (space in previous example) File usually separate each piece of info with a delimiter – any special character designating a new piece of data (space in previous example)

32 Tokenizing in Java Use a method of the String class called split Use a method of the String class called split Parameters: delimiting rules Parameters: delimiting rules Returns: An array of tokens Returns: An array of tokens We need to determine what delimiters are needed for each line. We need to determine what delimiters are needed for each line. Put them in a string that looks like this: Put them in a string that looks like this: “[ ]+” “[ ]+” “[,]+” “[,]+” “[ \n\t]+” “[ \n\t]+”

33 String Tokenizing in Java Scanner stdin = new Scanner(System.in); System.out.print("Enter a line with comma- separated integers(no space): " ); String input = stdin.nextLine(); String[] st = input.split(“[,]+”); for ( int i=0; i<st.length; i++ ) { int n = Integer.parseInt(st[i]); int n = Integer.parseInt(st[i]); System.out.println(n); System.out.println(n);}

34 What if I want to read this file? Class 1:8:10:7:6:5 Class 2:4:4:5:10:8:8:8 Class 3:6:7:9:10:7:5 Class 4:9:9:8:7:8 Class 5:9:10:9:3 Write a program to print out the average of the scores for each class

35 File gradeFile = new File(“scores.txt”); if(gradeFile.exists()) { Scanner inFile = new Scanner(gradeFile); while( inFile.hasNextLine() ) { String line = inFile.nextLine(); String[] st = line.split(“[:]+"); System.out.print(st[0] + “’s”); double sum = 0; for (int n=1; n<st.length; n++) sum += Integer.parseInt(st[n]); System.our.println(" average is "+ sum/(st.length-1)); }inFile.close();}

36 Modifications of Output Two constraints so far: Two constraints so far: Files are overwritten Files are overwritten Output is buffered and not written immediately Output is buffered and not written immediately But what if we want more control? But what if we want more control?

37 File Class java.io.FileWriter java.io.FileWriter Associated with File object Associated with File object Connects an output stream to write bytes of info Connects an output stream to write bytes of info Constructors Constructors FileWriter(, ); FileWriter(, ); true to append data, false to overwrite all of file true to append data, false to overwrite all of file This will overwrite an existing file This will overwrite an existing file To avoid, create File object and see if exists() is true To avoid, create File object and see if exists() is true

38 Java File Output PrintWriter PrintWriter composed from several objects composed from several objects PrintWriter out = new PrintWriter( new FileWriter( dstFileName, false ), true ); throws FileNotFoundException throws FileNotFoundException Methods Methods print(), println() : buffers data to write print(), println() : buffers data to write flush() : sends buffered output to destination flush() : sends buffered output to destination close() : flushes and closes stream close() : flushes and closes stream false: overwrite true: appends false: overwrite true: appends true: autoflush false: no autoflush true: autoflush false: no autoflush

39 Java File Output // Append to an existing file PrintWriter outFile1 = new PrintWriter( new FileWriter(dstFileName,true),false); // Autoflush on println PrintWriter outFile2 = new PrintWriter( new FileWriter(dstFileName,false),true); outFile1.println( “appended w/out flush” ); outFile2.println( “overwrite with flush” );

40 to flush or not to flush Advantage to flush: Advantage to flush: Safer – guaranteed that all of our data will write to the file Safer – guaranteed that all of our data will write to the file Disadvantage Disadvantage Less efficient – writing to file takes up time, more efficient to flush once (on close) Less efficient – writing to file takes up time, more efficient to flush once (on close) Can call flush( ) on a PrintWriter object created with just a filename at any time to force the buffer to disk Can call flush( ) on a PrintWriter object created with just a filename at any time to force the buffer to disk

41 Other Ways to Read/Write Files Binary files (InputStream/OutputStream) Binary files (InputStream/OutputStream) Storing readable text is rather inefficient (2 bytes/character, but we tend to use less than 100 letters) Storing readable text is rather inefficient (2 bytes/character, but we tend to use less than 100 letters) Images, music, class files use 0’s and 1’s directly Images, music, class files use 0’s and 1’s directly Can read and write these files, but we typically must work byte by byte Can read and write these files, but we typically must work byte by byte Random access (RandomAccessFile) Random access (RandomAccessFile) What if we don’t want to read from the beginning to the end? What if we don’t want to read from the beginning to the end? We have a “cursor” in the file, and can “seek” around to different points in any order, overwriting what was there before We have a “cursor” in the file, and can “seek” around to different points in any order, overwriting what was there before


Download ppt "Chapter 16 – Files and Streams. Announcements Only responsible for 16.1,16.3 Only responsible for 16.1,16.3 Other sections “encouraged” Other sections."

Similar presentations


Ads by Google