Download presentation
Presentation is loading. Please wait.
1
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input and Output
2
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 2 Acknowledgement Thanks to Paul Curzon for the programs described in this presentation You’ll meet Paul again if you do module CMT1501 next semester
3
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 3 File Input and Output Writing data to files Reading data from files
4
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 4 A problem Suppose I write a program to read in 200 exam marks and store them in an array, then work out the average mark and print it out. I run the program and enter the marks The marks are now in an array The program prints the average
5
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 5 Another problem Now I want to use another program which reads in 200 exam marks and stores them in an array, then finds and prints out the highest and lowest mark. I run the program, type in the marks and get the results… Now I want to use another program which reads in 200 exam marks and stores them in an array, then prints out the number of fails (marks < 40) WAIT a minute!
6
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 6 Solution We don’t want to type in 200 numbers every time we run a program. We should put the numbers in a file and get the programs to read them from the file rather than from the keyboard.
7
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 7 What’s a file? A collection of information, stored on a disk –hard disk –floppy –zip disk –CD-ROM There’s lots of different sorts –Word documents –Excel spreadsheets –Program source files (text) –executable (binary) files –Data files for programs
8
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 8 The nature of a file Files used by Java programs can be of lots of different types The type of files that we’ll use are files of characters We’ll need to be able to associate our program with the name of a file on disk, stating whether we will be writing to the file or reading from it. We will then need to use commands to read or write strings to and from the file.
9
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 9 Writing to a file To write data to a file on a disk we must do several steps create a file object in the program to represent the file and link it to the file (possibly creating the file if it does not already exist) create a buffer object to collect characters together before they are sent to the file and connect it to the file create a PrintWriter object that converts values of different types to a stream of characters and connect it to the buffer write the data to the file by passing it to the PrintWriter object close the file releasing it for others to use
10
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 10 Create file object final FileWriter outputFile = new FileWriter("wombat.dat"); This creates an object called outputFile of class Filewriter It’s the name used to refer to the file in the program... … a bit like a pipe connecting the program to the file … …through which flows a stream of characters to the file This object is associated with a real file on the disk called wombat.dat
11
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 11 Create buffer object final BufferedWriter outputBuffer = new BufferedWriter(outputFile); Creates a BufferedWriter object called outputBuffer and connects it to the output file. A BufferedWriter also takes a stream of characters. Sending characters to a file one at a time is very slow. A buffer is a place where a series of characters can be collected, then sent to the file all at once, which is more efficient. They sit in the buffer and are sent to the file when it is full.
12
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 12 Create PrintWriter object final PrintWriter printstream = new PrintWriter(outputBuffer); creates a PrintWriter object called printstream and links it to the outputBuffer we have just created This manages the conversion of strings, integers etc into streams of characters to be sent to the file PrintWriter objects have a method called println just as we have for printing to the screen.
13
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 13 A brief aside: Command window output We’ve been doing screen output using JOptionPane.showMessageDialog which produces output in a ‘box’
14
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 14 … and using System.out.println which produces output in the command window from which we can compile and run our Java programs with javac and java
15
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 15 What’s this got to do with file output? Well, System.out is the standard output object print and println are methods of this object, for writing strings to the command window Our PrintWriter object printstream is the analogous object for writing to our file printstream.println(”Hello"); writes Hello into our file.
16
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 16 The mechanism The message goes into the file wombat.dat, not to the screen. It does so by being converted into a stream of characters that are collected together in the buffer called outputBuffer before being forwarded on via outputFile to wombat.dat
17
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 17 Examples printstream.println(”Hello\nHello\nHello"); writes into the file Hello printstream.println( ”42 + “\n is the answer”); writes into the file 42 is the answer
18
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 18 And then? Finally we close the PrintWriter, BufferedWriter and Filewriter down, flushing any characters still waiting in the buffer into the file (if the buffer had not been filled they would still wait there) and freeing the file for others to use. This is done using the PrintWriter method called close, which automatically closes the buffer and file it is connected to. printstream.close();
19
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 19 To view the file In the command window, we can use the DOS type command to view the contents of the data file. If our program had used the instruction printstream.println(”Hello\nHello\nHello"); to put data into the file, then we can view it with C:> type wombat.dat Hello
20
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 20 To view the file We could also view the file using Notepad or Word
21
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 21 Reading from a file final FileReader inputFile = new FileReader("wombat.dat"); This creates a file object in the program, called inputFile. This one is a FileReader not a FileWriter object – it can get things out of a file not put them in. It’s associated with a real file on disk called “wombat.dat”.
22
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 22 Buffering final BufferedReader inputBuffer = new BufferedReader(inputFile); This creates a buffer connected to the FileReader object. Streams of characters from the file will be taken from the file and stored in the buffer until needed by the program.
23
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 23 Reading the data When the program wishes to read in some data from the file it issues a readLine command: String line1 = inputBuffer.readLine(); This reads a line of data from the file (up to the next new line character stored there) It converts the stream of single characters into a string object We have stored this string in the variable line1
24
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 24 Reading the data The readLine method reads all characters, up to the next new line character, into a string.
25
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 25 The exam marks problem First we need a program to input the marks from the user and write them into a file. We can store the marks into the file, one per line, with a loop which gets each mark from the user, then writes it into the file, terminating when the user enters some sentinel value such as -1, which could not be a valid exam mark.
26
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 26 Putting marks in file String input = JOptionPane.showInputDialog( ”Enter a number, -1 to stop: "); int num= Integer.parseInt(input); while (num != -1) { printstream.println(num+”\n"); input=JOptionPane.showInputDialog( ”Enter a number, -1 to stop: "); num= Integer.parseInt(input); }
27
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 27 Reading marks from file into an array This program also uses a loop; assume we know there are 200 marks String str; int marks[] = new int[200]; for (int i=0; i <= 199; i++) { str = inputBuffer.readLine(); marks[i] = Integer.parseInt(str); }
28
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 28 Processing We can now –work out the average –find the highest/ lowest marks –count the fails by processing the array Try it!
29
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 29 Finally... inputBuffer.close(); Closes the connection to the file as before See the module web site for example file- handling programs for input and output
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.