Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Click to edit Master title style
•
•
•
•
•
Click to edit Master text styles
Second level
Files
Third level
Fourth level
Chapter 8
Fifth level
1
Click to edit Files
Master title style
•
•
•
•
••
To use
Click
to files,
edit Master
use Filetext
class
styles
Second
File
class
level
can’t read/write
Third
To
read/write
level files, use
Scanner/PrintWriter
classes
Fourth level
File
Fifthinput
level can be text-based (what we’ll
do), stream-based
2
Click to edit
Text
Master
Input title style
Use
thetoScanner
classtext styles
• Click
edit Master
•• import
java.io.File;
Secondjava.util.Scanner;
level
•• Put
ThirdScanner
level activity within try { …} catch
(Exception e) {…}
• Fourth level
• Tie object to File object and diskfile name
•• Fifth
level
Use next to get next String, nextInt to get
nextInt, etc. [Must know order of input]
• Close the scanner object.
3
you must put Scanner
declaration in trycatch
Click to edit
Text
Master
Input title style
you must have
new File.
try
{
Scanner s = new Scanner
(new File("filename.txt"));
while (s.hasNext( )) // read to EOF
means input order is
{
String firstS = s.next( );
String, int, double for
int secondI = s.nextInt( );
each record
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);
}
•
•
•
•
•
Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
}
4
Click to edit
TextMaster
outputtitle style
• Click
to PrintWriter
edit Master text styles
Use
the
class.
••
••
•
•
Second
level
Import java.io.PrintWriter
Third
level
Put inside
Fourth level
try { …} catch (Exception e) {…}
Fifth level
• Tie PrintWriter object to diskfile name
• Use print, println to write to the object
• Close the FileWriter.
5
Click to edit
TextMaster
outputtitle style
try {
PrintWriter
writer text
= styles
• Click
to edit Master
new PrintWriter("name of file");
• Second
level is more text to write)
while(there
{
• Third
level
...
• Fourth
level
writer.println(next
piece of text);
...
• Fifth level
}
writer.close();
}
catch(IOException e) {
/* something went wrong with accessing the
file */
6
}
Write
a program
count number
of
Click
to edittoMaster
title style
lines in a file and write the number
• Click to edit
Master
text file
styles
to an
output
• Second level
java countlines myinfile.txt myoutfile.txt
• Third level
would return 2 if file is:
• Fourth level
This is a file
• Fifth
level
with two lines.
myinfile.txt
7
Click
Command
to edit Line
Master
Arguments
title style
•java
Click
countlines
to edit Master
myinfile.txt
text styles
myoutfile.txt
• Java
Second
puts
level
all commands in the array args
•public
Third static
level void main(String[] args)
• Fourth
args[0] level
is “myinfile.txt”
• Fifth
level
args[1]
is “myoutfile.txt”
8
Write a program to print all
Click to edit Master title style
command line arguments
public
PrintArgs
• Clickclass
to edit
Master text styles
{• Second level
static void main(String[] args)
• public
Third level
{
• Fourth level
for (String c : args)
• Fifth level
System.out.println(c);
}
}
9
Write
a program
count number
of
Click
to edittoMaster
title style
lines in a file and write the number
• Click to edit
Master
text file
styles
to an
output
• Second level
java countlines myinfile.txt myoutfile.txt
• Third level
would return 2 if file is:
• Fourth level
a file
• This
Fifthis level
with two lines.
myinfile.txt
10
import java.io.*;
Import java.util.Scanner;
Click to edit Master title style
public class Countlines
•{ Click to edit Master text styles
//
all methods
• put
Second
levelhere
public static void main(String[] args)
• Third
level
{
if (args.length
• Fourth
level != 2)
{
• FifthSystem.out.println
level
("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]);11
}
// constructor
public Countlines(String filein, String fileout)
{
int nbrLines = readfrom(filein);
writeTo(fileout,
nbrLines);text styles
• Click
to edit Master
}
Click to edit Master title style
• Second level
//
method
• write
Third
level
public void writeTo(String fileout, int nbrLines)
•{ Fourth level
{ level
• try
Fifth
PrintWriter outFile = new PrintWriter(new
File(fileout));
outFile.println("The number of lines is " + nbrLines);
outFile.close( );
}
catch (IOException e) { // do nothing}
}
12
Scanner
Click to Class:
edit Master
for reading
title style
files
public int readfrom(String filein)
{
Scanner scanner;
int countlines = 0;
• Second
level
try {
int countlines = 0;
• Third
level
scanner
= new Scanner(new File(filein));
while (scanner.hasNext())
{
String line = scanner.nextLine( );
• Fifth level
countlines++;
}
}
catch (FileNotFoundException ex) {
System.out.println("File not found ...aborting program.");
System.exit(1); }
return countlines;
}
• Click to edit Master text styles
• Fourth level
13
Another Example using Scanner
Click to edit Master title style
import java.util.*;
import java.io.*;
class MyScanner {
public MyScanner ( )
•{ Second level
Scanner scanner;
try {
• Third
level
scanner = new Scanner(new File("infile.java"));
int x = scanner.nextInt( );
int y = scanner.nextInt( );
• Fifth
level
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); }
}
}
• Click to edit Master text styles
• Fourth level
14
Click
Write
to edit
a Payroll
Master
Program
title style
Write a program that reads in a file in the form first last
• Click
to hoursworked
edit Masterexempt,
text styles
hourlyrate
e.g,:
Susie Jones 13.50 40 N
•Johnny
Second
Jacoblevel 32.30 60 Y
Jane White 35.00 55 N
• Third level
And writes out information to an output file in the form:
•first
Fourth
level e.g.,
last weeklypay,
Susie
Jones:
540.0
•Johnny
Fifth
level
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.
15
Click to
Toedit
Calculate
MasterPay
title style
Pay
should
beMaster
figuredtext
in the
following way: if
• Click
to edit
styles
the employee has worked no overtime,
• Second
level of hours * hourly rate.
pay is number
• Otherwise,
Third level if the employee is not exempt,
the pay is hourly rate * 40 + hourly rate *
• Fourth
levelover 40. If the employee is
1.5 * hours
• exempt,
Fifth levelthe employee receives no
overtime (so is paid for no more than 40
hours, less if the employee worked less).
16