cop 3503 fall 2012 shayan javed lecture 12

66
1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1

Upload: hei

Post on 16-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 12. Programming Fundamentals using Java. Input/Output (IO). I/O. So far we have looked at modeling classes. I/O. So far we have looked at modeling classes Not much in the way of Input. Input. 3 ways of providing input to the program:. Input. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 12

1 / 651

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 12

Programming Fundamentals using Java

Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 12

2 / 65

Input/Output (IO)

Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 12

3 / 65

I/O

So far we have looked at modeling classes

Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 12

4 / 65

I/O

So far we have looked at modeling classes

Not much in the way of Input...

Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 12

5 / 65

Input

3 ways of providing input to the program:

Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 12

6 / 65

Input

3 ways of providing input to the program: Pass parameters directly to the program

Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 12

7 / 65

Input

3 ways of providing input to the program: Pass parameters directly to the program

Command-line input from the user

Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 12

8 / 65

Input

3 ways of providing input to the program: Pass parameters directly to the program

Command-line input from the user

Reading in files

Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 12

9 / 65

Passing parameters

When running the program can directly pass parameters to it

Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 12

10 / 65

Passing parameters

When running the program can directly pass parameters to it

java ProgramName parameter1 parameter2 ...

Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 12

11 / 65

Passing parameters

public static void main(String[] args) {

// args is the array of all parameters

// args[0] would be the first parameter

}

Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 12

12 / 65

Passing parameters

public static void main(String[] args) {

// args is the array of all parameters

// args[0] would be the first parameter

}

Let’s look at an example

Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 12

13 / 65

Command-line input

Receive input from the console during execution

Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 12

14 / 65

Command-line input

Receive input from the console during execution

Use the Scanner class

Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 12

15 / 65

The Scanner class

Used for reading data

Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 12

16 / 65

The Scanner class

Used for reading data

Constructors: Scanner(InputStream)

InputStream = System.in

Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 12

17 / 65

The Scanner class

Used for reading data

Constructors: Scanner(InputStream)

InputStream = System.in Scanner(String)

Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 12

18 / 65

The Scanner class

Used for reading data

Constructors: Scanner(InputStream)

InputStream = System.in Scanner(String) Scanner(File)

Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 12

19 / 65

The Scanner class

Methods: boolean hasNext() : If scanner has more

tokens

Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 12

20 / 65

The Scanner class

Methods: boolean hasNext() : If scanner has more

tokens String next() : Returns the next String int nextInt() : Returns the next int double nextDouble() : Returns the next double

Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 12

21 / 65

The Scanner class

Methods: boolean hasNext() : If scanner has more

tokens String next() : Returns the next String int nextInt() : Returns the next int double nextDouble() : Returns the next double void useDelimiter(pattern: String) : Set’s the delimiting pattern

(“ “ by default)

Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 12

22 / 65

The Scanner class

Methods: boolean hasNext() : If scanner has more

tokens String next() : Returns the next String int nextInt() : Returns the next int double nextDouble() : Returns the next double void useDelimiter(pattern: String) : Set’s the delimiting pattern

(“ “ by default)

void close(): Closes the Scanner

Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 12

23 / 65

Command-line input

Use the “next..” methods to read from the standard input

Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 12

24 / 65

Command-line input

Use the “next..” methods to read from the standard input

import java.util.Scanner;

Scanner scanner = new Scanner(System.in);

System.out.print(“Enter number1: “);

double number1 = scanner.nextDouble();

System.out.print(“Enter number2: “);

double number2 = scanner.nextDouble();

System.out.println(“The addition of the two numbers: “ + (number1 + number2));

Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 12

25 / 65

File Input

Ability to read files essential to any language.

Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 12

26 / 65

File Input

Ability to read files essential to any language.

Two ways to store data: Text format:

Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 12

27 / 65

File Input

Ability to read files essential to any language.

Two ways to store data: Text format:

Human-readable form Can be read by text editors

Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 12

28 / 65

File Input

Ability to read files essential to any language.

Two ways to store data: Text format:

Human-readable form Can be read by text editors

Binary format: Used for executable programs Cannot be read by text editors

Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 12

29 / 65

The File class

java.io package

Represents a “file” object

Used for input/output through data streams, the file system and serialization.

Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 12

30 / 65

The File class

Constructors: File(pathname: String): Creates a File object for the specified

pathname. pathname = directory or file

Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 12

31 / 65

The File class

Constructors: File(pathname: String): Creates a File object for the specified

pathname. pathname = directory or file

File(parent: String, child: String): Creates a File object for the childunder the directory parent.

child may be a filename or subdirectory.

Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 12

32 / 65

The File class

Methods: boolean exists() : If the file exists

Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 12

33 / 65

The File class

Methods: boolean exists() : If the file exists boolean canRead() : If the file exists and we can read it boolean canWrite() : If the file exists and we can write to it

Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 12

34 / 65

The File class

Methods: boolean exists() : If the file exists boolean canRead() : If the file exists and we can read it boolean canWrite() : If the file exists and we can write to it void isDirectory() : if the object is a directory void isFile() : if the object is a file

Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 12

35 / 65

The File class

Methods: boolean exists() : If the file exists boolean canRead() : If the file exists and we can read it boolean canWrite() : If the file exists and we can write to it void isDirectory() : if the object is a directory void isFile() : if the object is a file String getName() : Returns the name of the file

Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 12

36 / 65

The File class

Methods: boolean exists() : If the file exists boolean canRead() : If the file exists and we can read it boolean canWrite() : If the file exists and we can write to it void isDirectory() : if the object is a directory void isFile() : if the object is a file String getName() : Returns the name of the file boolean delete() : Deletes the file and returns true

if succeeded renameTo (dest: File) : Tries to rename the file and returns true

if succeeded

Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 12

37 / 65

Reading Files

Use the Scanner class

new Scanner(File)

Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 12

38 / 65

Reading Files

How does Scanner really work?

Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 12

39 / 65

Reading Files

How does Scanner really work?

Breaks file contents into tokens Uses a delimiter

Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 12

40 / 65

Reading Files

How does Scanner really work?

Breaks file contents into tokens Uses a delimiter Delimiter by default is whitespace

Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 12

41 / 65

Reading Files

How does Scanner really work?

Breaks file contents into tokens Uses a delimiter Delimiter by default is whitespace

Reads a token, converts it to the required type

Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 12

42 / 65

Reading Files

How does Scanner really work?

Breaks file contents into tokens Uses a delimiter Delimiter by default is whitespace

Reads a token, converts it to the required type

Can change the delimiter – useDelimiter() method

Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 12

43 / 65

Reading Files

// Reads in the file and outputs all the tokens

Scanner input = new Scanner(new File(“test.txt”));

while (input.hasNext()) {

System.out.println(input.next());

}

Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 12

44 / 65

Reading Files

// Reads in the file and outputs all the tokens

Scanner input = new Scanner(new File(“test.txt”));

while (input.hasNext()) {

System.out.println(input.next());

}

ERROR – WON’T COMPILE

Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 12

45 / 65

Reading Files

// Reads in the file and outputs all the tokens

Scanner input = new Scanner(new File(“test.txt”));

while (input.hasNext()) {

System.out.println(input.next());

}

ERROR – WON’T COMPILE

The constructor throws a FileNotFoundException

Page 46: COP 3503 FALL 2012 Shayan Javed Lecture 12

46 / 65

Reading Files

// Reads in the file and outputs all the tokens

try {

Scanner input = new Scanner(new File(“test.txt”));

while (input.hasNext()) {

System.out.println(input.next());

}

} catch (FileNotFoundException fe) {

fe.printStackTrace();

}

Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 12

47 / 65

Reading files

Have to be careful. Suppose a file contains the line: 34 567

Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 12

48 / 65

Reading files

Have to be careful. Suppose a file contains the line: 34 567

What will be the contents of intValue and line after the following code is executed?

Scanner in = new Scanner(new File(“test.txt”));

int intValue = in.nextInt();

String line = in.nextLine();

Page 49: COP 3503 FALL 2012 Shayan Javed Lecture 12

49 / 65

Reading files

Scanner scanner = new Scanner(“file.txt”);

Treats the String “file.txt” as the source, NOT the file “file.txt”

Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 12

50 / 65

Writing Files

Use the PrintWriter class

Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 12

51 / 65

Writing Files

Use the PrintWriter class

Constructors: PrintWriter(File file): Creates a PrintWriter for the specified

File

PrintWriter(String name): Creates a PrintWriter for the specified File with the name

Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 12

52 / 65

The PrintWriter class

Methods: void print(String) : Writes a String

Page 53: COP 3503 FALL 2012 Shayan Javed Lecture 12

53 / 65

The PrintWriter class

Methods: void print(String) : Writes a String void print(int) : Writes an int void print(float) : Writes a float

Page 54: COP 3503 FALL 2012 Shayan Javed Lecture 12

54 / 65

The PrintWriter class

Methods: void print(String) : Writes a String void print(int) : Writes an int void print(float) : Writes a float void println(String) : Writes a String but also adds a line

separator

Page 55: COP 3503 FALL 2012 Shayan Javed Lecture 12

55 / 65

The PrintWriter class

Methods: void print(String) : Writes a String void print(int) : Writes an int void print(float) : Writes a float void println(String) : Writes a String but also adds a line

separator void flush() : Flushes the output stream. Ensures

writing to the file

Page 56: COP 3503 FALL 2012 Shayan Javed Lecture 12

56 / 65

The PrintWriter class

Methods: void print(String) : Writes a String void print(int) : Writes an int void print(float) : Writes a float void println(String) : Writes a String but also adds a line

separator void flush() : Flushes the output stream. Ensures

writing to the file void close() : Closes the output stream.

Page 57: COP 3503 FALL 2012 Shayan Javed Lecture 12

57 / 65

Writing FilesPrintWriter output = null;

try {

output = new PrintWriter(new File(“test”));

// creates a file if it does not exist;

// discards the current content if the file exists

output.print("John T Smith "); output.println(90);

output.print("Eric K Jones "); output.println(85);

output.flush();

}

catch(IOException ioe) {

System.out.println(ioe.toString());

}

finally { if (output != null) output.close(); }

Page 58: COP 3503 FALL 2012 Shayan Javed Lecture 12

58 / 65

Writing Files

Problem: What if you want to append to the file not replace it?

Page 59: COP 3503 FALL 2012 Shayan Javed Lecture 12

59 / 65

Writing Files

Problem: What if you want to append to the file not replace it?

Solution 1: Read the whole file, then write it back.

Page 60: COP 3503 FALL 2012 Shayan Javed Lecture 12

60 / 65

Writing Files

Problem: What if you want to append to the file not replace it?

Solution 1: Read the whole file, then write it back. Cumbersome and too much work

Page 61: COP 3503 FALL 2012 Shayan Javed Lecture 12

61 / 65

Writing Files

Problem: What if you want to append to the file not replace it?

Solution 1: Read the whole file, then write it back. Cumbersome and too much work

Solution 2: Use the FileWriter class

Page 62: COP 3503 FALL 2012 Shayan Javed Lecture 12

62 / 65

Writing Files

Problem: What if you want to append to the file not replace it?

Solution 1: Read the whole file, then write it back. Cumbersome and too much work

Solution 2: Use the FileWriter class FileWriter(File file, boolean append)

Page 63: COP 3503 FALL 2012 Shayan Javed Lecture 12

63 / 65

Writing Files

Problem: What if you want to append to the file not replace it?

Solution 1: Read the whole file, then write it back. Cumbersome and too much work

Solution 2: Use the FileWriter class FileWriter(File file, boolean append) PrintWriter pw = new PrintWriter(new FileWriter(file,

true) )

Page 64: COP 3503 FALL 2012 Shayan Javed Lecture 12

64 / 65

Writing FilesPrintWriter output = null;

try {

output = new PrintWriter(new File(“test”));

// creates a file if it does not exist;

// discards the current content if the file exists

output.print("John T Smith "); output.println(90);

output.print("Eric K Jones "); output.println(85);

output.flush();

}

catch(IOException ioe) {

System.out.println(ioe.toString());

}

finally { if (output != null) output.close(); }

Page 65: COP 3503 FALL 2012 Shayan Javed Lecture 12

65 / 65

Writing Files – AppendPrintWriter output = null;

try {

output = new PrintWriter( new FileWriter(new File(“test”), true) );

// creates a file if it does not exist;

// discards the current content if the file exists

output.print("John T Smith "); output.println(90);

output.print("Eric K Jones "); output.println(85);

output.flush();

}

catch(IOException ioe) {

System.out.println(ioe.toString());

}

finally { if (output != null) output.close(); }

Page 66: COP 3503 FALL 2012 Shayan Javed Lecture 12

66 / 65

Summary

Use Scanner for reading from command-line and files. Based on delimiters

Use PrintWriter for writing to files