5/18/20 INTRO TO OOP FOR STREAMS AND DATA SCIENCE FILES PROF. JOHN GAUCH OVERVIEW OVERVIEW OVERVIEW § Many programs are "data processing" applications § Files are very useful for data processing applications § Read the input data § Files provide long term storage of valuable information § Perform sequence of operations on this data § Files can contain large quantities of data § Write the output data § Files can be viewed and modified by text editors § Files can be read and written by programs § How we read and write this data is a key part of program § In this section, we will show how § We use System.in and Scanner to read keyboard input § FileInputStream and Scanner are used to read files § We use System.out,println to print output to screen § FileOutputStream and PrintWriter are used to write files § Having users type in their data is very limiting § We need files to process larger quantities of data 3 4 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 OVERVIEW STREAMS AND § Lesson objectives: FILES § Learn more about input and output streams § Learn how open and close text files § Learn how to read and write text files § Learn about input / output error checking § Study programs for numerical data input/output § Study programs for mixed data input/output PART 1 INPUT FILES 5 (c) Prof. John Gauch, Univ. of Arkansas, 2020 1
5/18/20 INPUT FILES READING INTEGERS § Input files have many advantages § Consider the problem of reading and processing an input file that contains integers separated by spaces § We can store large amounts of data in a file § Get the name of the file to open § We can store different kinds of data in a file § Create a FileInputStream object § We can edit this data using a text editor § Create a Scanner object § We can read and process this data in a program § While data is available to read § Read integer value from the input file § Java has provided support for file input § Process this data in some way § Add the following at top of program § Close the input file import java.io.FileInputStream; import java.io.IOException; § Java will “throw exceptions” (print error message and die) if the file does not exist, or if you try to read past end of file 7 8 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 READING INTEGERS READING INTEGERS § Program to read and print integer values in a file § Program to read and print integer values in a file // Create file stream and scanner // Create file stream and scanner FileInputStream fileStream = new FileInputStream(fileName); FileInputStream fileStream = new FileInputStream(fileName); Scanner fileScanner = new Scanner(fileStream); Scanner fileScanner = new Scanner(fileStream); // Loop reading and printing data // Loop reading and printing data while (fileScanner.hasNextInt()) while (fileScanner.hasNextInt()) { { This creates a Scanner This checks the scanner int value = fileScanner.nextInt(); int value = fileScanner.nextInt(); object we can use to read to see if another integer System.out.print(value + " "); System.out.print(value + " "); any data type from the file is available in file to read } } // Close input file // Close input file fileStream.close(); fileStream.close(); 9 10 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 READING INTEGERS READING INTEGERS § Program to read and print integer values in a file § Program to read and print integer values in a file // Create file stream and scanner // Create file stream and scanner FileInputStream fileStream = new FileInputStream(fileName); FileInputStream fileStream = new FileInputStream(fileName); Scanner fileScanner = new Scanner(fileStream); Scanner fileScanner = new Scanner(fileStream); // Loop reading and printing data // Loop reading and printing data while (fileScanner.hasNextInt()) while (fileScanner.hasNextInt()) { { This reads and prints int value = fileScanner.nextInt(); int value = fileScanner.nextInt(); the next integer from System.out.print(value + " "); System.out.print(value + " "); the input file } } Once the input/output is working we can add // Close input file // Close input file more data processing fileStream.close(); fileStream.close(); here (e.g. calculate the average value) 11 12 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 2
5/18/20 READING INTEGERS READING INTEGERS § Program to read and print integer values in a file § Sample input.txt file (all values on one line) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 // Create file stream and scanner FileInputStream fileStream = new FileInputStream(fileName); Scanner fileScanner = new Scanner(fileStream); § Sample input.txt file (five values per line) // Loop reading and printing data 1 2 3 4 5 while (fileScanner.hasNextInt()) 6 7 8 9 10 11 12 13 14 15 { 16 17 18 19 20 int value = fileScanner.nextInt(); System.out.print(value + " "); § It does not matter how this input file is formatted because } This closes the input fileScanner.nextInt() will skip over all white space before file so it can be used // Close input file reading the integer by other users fileStream.close(); 13 14 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 CODE DEMO READING STRINGS § Consider the problem of reading an essay and counting the number of times a target word occurs (e.g. “because”) § Get the name of the file to open § Create a FileInputStream object Compile and run FindAverage.java § Create a Scanner object § Get target word from user § While data is available to read § Read string from the input file § Compare string to target word § If word matches increment counter § Close the input file 15 16 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 READING STRINGS READING STRINGS § Program to read and compare strings in a file § Program to read and compare strings in a file // Read file name // Read file name System.out.print("Enter file name: "); System.out.print("Enter file name: "); String fileName = scnr.next(); String fileName = scnr.next(); First we get user input Then we create Scanner // Read target word // Read target word object to read strings from System.out.print("Enter target word: "); System.out.print("Enter target word: "); the input file one by one String target = scnr.next(); String target = scnr.next(); // Create file stream and scanner // Create file stream and scanner FileInputStream fileStream = new FileInputStream(fileName); FileInputStream fileStream = new FileInputStream(fileName); Scanner fileScnr = new Scanner(fileStream); Scanner fileScnr = new Scanner(fileStream); 17 18 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 3
5/18/20 READING STRINGS READING STRINGS § Program to read and compare strings in a file § Program to read and compare strings in a file // Read and print words // Read and print words String word; String word; int count = 0; int count = 0; int found = 0; int found = 0; while (fileScnr.hasNext()) while (fileScnr.hasNext()) Loop reading words until we If the word matches the target { { reach the end of the input file word we increment the counter word = fileScnr.next(); word = fileScnr.next(); if (word.equals(target)) if (word.equals(target)) found++; found++; count++; count++; } } 19 20 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 READING STRINGS READING STRINGS § Program to read and compare strings in a file § Sample book.txt input file (from David Copperfield) // Print results Whether I shall turn out to be the hero of my own life, or whether that System.out.println("Word '" + target + "' was found " + found + station will be held by anybody else, these pages must show. To begin " times out of " + count + " words in document"); my life with the beginning of my life, I record that I was born (as I have been informed and believe) on a Friday, at twelve o’clock at night. It // Close input file was remarked that the clock began to strike, and I began to cry, Print the results fileStream.close(); simultaneously. Close the input file 21 22 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 READING STRINGS CODE DEMO § Sample program output Enter file name: book.txt Enter target word: the Word 'the' found 3 times out of 73 words Compile and run CountWords.java Enter file name: book.txt Enter target word: I Word ’I' found 5 times out of 73 words Enter file name: book.txt Enter target word: zebra Word ’zebra' found 0 times out of 73 words 23 24 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 4
Recommend
More recommend