Download presentation
Presentation is loading. Please wait.
1
March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class Random NumbersRandom Numbers Output filesOutput files Input filesInput files Informal Lab 9Informal Lab 9
2
March 2005 2/18R. Smith - University of St Thomas - Minnesota Random Numbers The Random class (not this one)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));
3
March 2005 3/18R. Smith - University of St Thomas - Minnesota Working with Files We use the same methods as System.in/outWe use the same methods as System.in/out –println, print, printf, Scanner class We must “open” and “close” filesWe must “open” and “close” files –“Open” when we create the file object –“close()” method when done
4
March 2005 4/18R. Smith - University of St Thomas - Minnesota Output Files PrintWriter classPrintWriter class –“new” takes file name as an argument –Creates that file Use the object with ‘print’ methodsUse the object with ‘print’ methods Write to a fileWrite to a file –import java.io.*; –public static void main(String[] args) throws IOException –PrintWriter pwa; –pwa = new PrintWriter(“existingfile.txt”); Example: write student namesExample: write student names
5
March 2005 5/18R. Smith - University of St Thomas - Minnesota Append to Output File Use an extra object for the existing fileUse an extra object for the existing file “FileWriter” object“FileWriter” object FileWriter fw;// to define the existing fileFileWriter fw;// to define the existing file PrintWriter pwa;// to open for print functionsPrintWriter pwa;// to open for print functions fw = new FileWriter(“existingfile.txt”, true);fw = new FileWriter(“existingfile.txt”, true); pwa = new PrintWriter(fw);pwa = new PrintWriter(fw);
6
March 2005 6/18R. Smith - University of St Thomas - Minnesota Input Files Use Scanner class and File classUse Scanner class and File class import java.util.Scanner;import java.util.Scanner; import java.io.*;import java.io.*; public static void main(String[] args) throws IOExceptionpublic static void main(String[] args) throws IOException File fn; // identify the file to readFile fn; // identify the file to read Scanner sc; // for reading the dataScanner sc; // for reading the data fn = new File(“filename.txt”);fn = new File(“filename.txt”); sc = new Scanner(fn);sc = new Scanner(fn);
7
March 2005 7/18R. Smith - University of St Thomas - Minnesota Program to assign numbers Collect the file nameCollect the file name Open the file/create File objectOpen the file/create File object –If not exists, write error messag. –ELSE Set up the scannerSet up the scanner Loop while “hasNext()Loop while “hasNext() –Read student name from file –Get random ID number –Println ID number and student name End loopEnd loop Done!Done!
8
March 2005 8/18R. Smith - University of St Thomas - Minnesota Some Examples Read student names, substitute in text.Read student names, substitute in text. –Use hasNext() method to check for end of file Collect the name of the file from the userCollect the name of the file from the user –Use exists() method to check for a ‘real’ file Mini-Lab 9:Mini-Lab 9: Calculate the average of some numbers in a fileCalculate the average of some numbers in a file –Use hasNext() and exists()
9
March 2005 9/18R. Smith - University of St Thomas - Minnesota That’s it. Questions?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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.