1 / 16

Files

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;

terrel
Télécharger la présentation

Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files Chapter 8

  2. 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

  3. 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.

  4. 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 }

  5. 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.

  6. 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 */ }

  7. 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

  8. 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”

  9. 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); } }

  10. 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

  11. 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]); } }

  12. // 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} }

  13. 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; }

  14. 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); } } }

  15. 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.

  16. 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).

More Related