Download presentation
Presentation is loading. Please wait.
1
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Files Section 2.13, Section 8.8
2
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 4 Text Input 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); } you must have new File. you must put Scanner declaration in try- catch means input order is String, int, double for each record }
5
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 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
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 8 import java.io.*; public class countlines { // all methods that do something inserted 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]); }
9
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 9 // constructor public countlines(String filein, String fileout) { int nbrLines = readfrom(filein); writeTo(fileout, nbrLines); } // write method public void writeTo(String fileout, int nbrLines) { PrintWriter outFile = new PrintWriter(new File(fileout)); fileout.println("The number of lines is " + nbrLines); fileout.close( ); }
10
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 10 Scanner Class: for reading files public int readfrom(String filein) { Scanner scanner; try { scanner = new Scanner(new File(filein)); while (scanner.hasNext) { String line = scanner.nextLine( ); // also next( ), nextInt( ), nextDouble( ) System.out.println(line); // for debugging only; countlines++; } return countlines; } catch (FileNotFoundException ex) { System.out.println("File not found...aborting program."); System.exit(1); } }
11
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 11 Console input: Section 2.13 Scanner class: Java 5 import java.util.*; class MyScanner { public MyScanner ( ) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number: "); int x = scanner.nextInt(); int y = scanner.nextInt(); System.out.println("The sum of " + x + " + " + y + " is " + (x+y)); }
12
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 12 File input 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); } }
13
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 13 Write a Payroll Program Write a program that reads in a file in the form first last hourlyrate hoursworked exempt, e.g,: Susie Jones13.5040 N Johnny Jacob32.3060 Y Jane White35.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. 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).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.