160 likes | 325 Views
Files. Chapter 8. Files. To use files, use File class File class can’t read/write To read/write files, use Scanner/PrintWriter classes File input can be text-based (what we’ll do), stream-based. Text Input. Use the Scanner class import java.util.Scanner; java.io.File;
E N D
Files Chapter 8
Files • To use files, use File class • File class can’t read/write • To read/write files, use Scanner/PrintWriter classes • File input can be text-based (what we’ll do), stream-based
Text Input Use the Scanner class • import java.util.Scanner; java.io.File; • Put Scanner activity within try { …} catch (Exception e) {…} • Tie object to File object and diskfile name • Use next to get next String, nextInt to get nextInt, etc. [Must know order of input] • Close the scanner object.
you must put Scanner declaration in try-catch Text Input you must have new File. try { Scanner s = new Scanner (new File("filename.txt")); while (s.hasNext( )) // read to EOF { String firstS = s.next( ); int secondI = s.nextInt( ); double thirdD = s.nextDouble( ); // do something with what you just read } } catch (Exception except) { // do what happens when things go tragically awry System.out.println("Things went tragically awry"); System.exit(1); } means input order is String, int, double for each record }
Text output Use the PrintWriter class. • Import java.io.PrintWriter • Put inside try { …} catch (Exception e) {…} • Tie PrintWriter object to diskfile name • Use print, println to write to the object • Close the FileWriter.
Text output try { PrintWriter writer = new PrintWriter("name of file"); while(there is more text to write) { ... writer.println(next piece of text); ... } writer.close(); } catch(IOException e) { /* something went wrong with accessing the file */ }
Write a program to count number of lines in a file and write the number to an output file java countlines myinfile.txt myoutfile.txt would return 2 if file is: This is a file with two lines. myinfile.txt
Command Line Arguments java countlines myinfile.txt myoutfile.txt • Java puts all commands in the array args public static void main(String[] args) • args[0] is “myinfile.txt” • args[1] is “myoutfile.txt”
Write a program to print all command line arguments public class PrintArgs { public static void main(String[] args) { for (String c : args) System.out.println(c); } }
Write a program to count number of lines in a file and write the number to an output file java countlines myinfile.txt myoutfile.txt would return 2 if file is: This is a file with two lines. myinfile.txt
import java.io.*; Import java.util.Scanner; public class Countlines { // put all methods here public static void main(String[] args) { if (args.length != 2) { System.out.println ("to run, type: 'java countlines filein fileout'"); System.exit(1); } // create a new countline object with first arg as input file, second as output Countlines cl = new Countlines(args[0], args[1]); } }
// constructor public Countlines(String filein, String fileout) { int nbrLines = readfrom(filein); writeTo(fileout, nbrLines); } // write method public void writeTo(String fileout, int nbrLines) { try { PrintWriter outFile = new PrintWriter(new File(fileout)); outFile.println("The number of lines is " + nbrLines); outFile.close( ); } catch (IOException e) { // do nothing} }
Scanner Class: for reading files public int readfrom(String filein) { Scanner scanner; int countlines = 0; try { int countlines = 0; scanner = new Scanner(new File(filein)); while (scanner.hasNext()) { String line = scanner.nextLine( ); countlines++; } } catch (FileNotFoundException ex) { System.out.println("File not found ...aborting program."); System.exit(1); } return countlines; }
Another Example using Scanner import java.util.*; import java.io.*; class MyScanner { public MyScanner ( ) { Scanner scanner; try { scanner = new Scanner(new File("infile.java")); int x = scanner.nextInt( ); int y = scanner.nextInt( ); System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); } catch (FileNotFoundException ex) { System.out.println("File not found ...aborting program."); System.exit(1); } } }
Write a Payroll Program Write a program that reads in a file in the form first last hourlyrate hoursworked exempt, e.g,: Susie Jones 13.50 40 N Johnny Jacob 32.30 60 Y Jane White 35.00 55 N And writes out information to an output file in the form: first last weeklypay, e.g., Susie Jones: 540.0 Johnny Jacob: 1292.0 Jane White: 2187.5 Your program should get the filein and fileout names from the command line. Print the total payroll for the week in a terminal window.
To Calculate Pay Pay should be figured in the following way: if the employee has worked no overtime, pay is number of hours * hourly rate. Otherwise, if the employee is not exempt, the pay is hourly rate * 40 + hourly rate * 1.5 * hours over 40. If the employee is exempt, the employee receives no overtime (so is paid for no more than 40 hours, less if the employee worked less).