File I/O, Randomness - University of St. Thomas
Download
Report
Transcript File I/O, Randomness - University of St. Thomas
QMCS 230: Today in Class
•
•
•
•
Random Numbers
Output files
Input files
Informal Lab 9
March 2005
R. Smith - University of St Thomas - Minnesota
1/18
Random Numbers
• The Random class (not this one)
– import java.util.Random;
– …
– Random rand = new Random();
– System.out.println(“Random double: “+rand.nextDouble());
– System.out.println(“Random int : “+rand.nextInt());
– System.out.println(“Random small int : “+rand.nextInt(25));
March 2005
R. Smith - University of St Thomas - Minnesota
2/18
Working with Files
• We use the same methods as
System.in/out
– println, print, printf, Scanner class
• We must “open” and “close” files
– “Open” when we create the file object
– “close()” method when done
March 2005
R. Smith - University of St Thomas - Minnesota
3/18
Output Files
• PrintWriter class
– “new” takes file name as an argument
– Creates that file
• Use the object with ‘print’ methods
• Write to a file
–
–
–
–
import java.io.*;
public static void main(String[] args) throws IOException
PrintWriter pwa;
pwa = new PrintWriter(“existingfile.txt”);
• Example: write student names
March 2005
R. Smith - University of St Thomas - Minnesota
4/18
Append to Output File
• Use an extra object for the existing file
• “FileWriter” object
• FileWriter fw;
• PrintWriter pwa;
// to define the existing file
// to open for print functions
• fw = new FileWriter(“existingfile.txt”, true);
• pwa = new PrintWriter(fw);
March 2005
R. Smith - University of St Thomas - Minnesota
5/18
Input Files
• Use Scanner class and File class
• import java.util.Scanner;
• import java.io.*;
• public static void main(String[] args) throws IOException
• File fn;
// identify the file to read
• Scanner sc; // for reading the data
• fn = new File(“filename.txt”);
• sc = new Scanner(fn);
March 2005
R. Smith - University of St Thomas - Minnesota
6/18
Program to assign numbers
• Collect the file name
• Open the file/create File object
– If not exists, write error messag.
– ELSE
• Set up the scanner
• Loop while “hasNext()
– Read student name from file
– Get random ID number
– Println ID number and student name
• End loop
• Done!
March 2005
R. Smith - University of St Thomas - Minnesota
7/18
Some Examples
• Read student names, substitute in text.
– Use hasNext() method to check for end of file
• Collect the name of the file from the user
– Use exists() method to check for a ‘real’ file
• Mini-Lab 9:
• Calculate the average of some numbers in a file
– Use hasNext() and exists()
March 2005
R. Smith - University of St Thomas - Minnesota
8/18
That’s it.
• Questions?
Creative Commons License
This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States
License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/
or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California,
94105, USA.
March 2005
R. Smith - University of St Thomas - Minnesota
9/18