1 1 COMP200 INPUT/OUTPUT OOP using Java, based on slides by Shayan Javed
2 Input/Output (IO)
3 I/O � So far we have looked at modeling classes
4 I/O � So far we have looked at modeling classes � Not much in the way of Input...
5 Input � 3 ways of providing input to the program:
6 Input � 3 ways of providing input to the program: � Pass parameters directly to the program
7 Input � 3 ways of providing input to the program: � Pass parameters directly to the program � Command-line input from the user
8 Input � 3 ways of providing input to the program: � Pass parameters directly to the program � Command-line input from the user � Reading in files
9 Passing parameters � When running the program can directly pass parameters to it
10 Passing parameters � When running the program can directly pass parameters to it java ProgramName parameter1 parameter2 ...
11 Passing parameters public static void main(String[] args) { // args is the array of all parameters // args[0] would be the first parameter }
12 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
13 Command-line input � Receive input from the console during execution
14 Command-line input � Receive input from the console during execution � Use the Scanner class
15 The Scanner class � Used for reading data
16 The Scanner class � Used for reading data � Constructors: � Scanner(InputStream) ■ InputStream = System.in
17 The Scanner class � Used for reading data � Constructors: � Scanner(InputStream) ■ InputStream = System.in � Scanner(String)
18 The Scanner class � Used for reading data � Constructors: � Scanner(InputStream) ■ InputStream = System.in � Scanner(String) � Scanner(File)
19 The Scanner class � Methods: � boolean hasNext() : If scanner has more tokens
20 The Scanner class � Methods: If scanner has more tokens � boolean hasNext() : Returns the next String � String next() : � int nextInt() : Returns the next int � double nextDouble() : Returns the next double
21 The Scanner class � Methods: If scanner has more tokens � boolean hasNext() : Returns the next String � String next() : � int nextInt() : Returns the next int Returns the next double � double nextDouble() : � void useDelimiter(pattern: String) : Set’s the delimiting pattern (“ “ by default)
22 The Scanner class � Methods: If scanner has more tokens � boolean hasNext() : Returns the next String � String next() : � int nextInt() : Returns the next int Returns the next double � double nextDouble() : � void useDelimiter(pattern: String) : Set’s the delimiting pattern (“ “ by default) � void close(): Closes the Scanner
23 Command-line input � Use the “next..” methods to read from the standard input
24 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));
25 File Input � Ability to read files essential to any language.
26 File Input � Ability to read files essential to any language. � Two ways to store data: � Text format:
27 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
28 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
29 The File class � java.io package � Represents a “file” object � Used for input/output through data streams, the file system and serialization.
30 The File class � Constructors: Creates a File object for the specified � File(pathname: String): pathname. pathname = directory or file
31 The File class � Constructors: Creates a File object for the specified � File(pathname: String): pathname. pathname = directory or file � File(parent: String, child: String): Creates a File object for the child under the directory parent. child may be a filename or subdirectory.
32 The File class � Methods: If the file exists � boolean exists() :
33 The File class � Methods: If the file exists � boolean exists() : If the file exists and we can read it � boolean canRead() : � boolean canWrite() : If the file exists and we can write to it
34 The File class � Methods: If the file exists � boolean exists() : If the file exists and we can read it � boolean canRead() : � boolean canWrite() : If the file exists and we can write to it if the object is a directory � void isDirectory() : if the object is a file � void isFile() :
35 The File class � Methods: If the file exists � boolean exists() : If the file exists and we can read it � boolean canRead() : � boolean canWrite() : If the file exists and we can write to it if the object is a directory � void isDirectory() : if the object is a file � void isFile() : Returns the name of the file � String getName() :
36 The File class � Methods: If the file exists � boolean exists() : If the file exists and we can read it � boolean canRead() : If the file exists and we can write to it � boolean canWrite() : if the object is a directory � void isDirectory() : if the object is a file � void isFile() : Returns the name of the file � String getName() : Deletes the file and returns true � boolean delete() : if succeeded Tries to rename the file and returns true � renameTo (dest: File) : if succeeded
37 Reading Files � Use the Scanner class � new Scanner(File)
38 Reading Files � How does Scanner really work?
39 Reading Files � How does Scanner really work? � Breaks file contents into tokens � Uses a delimiter
40 Reading Files � How does Scanner really work? � Breaks file contents into tokens � Uses a delimiter � Delimiter by default is whitespace
41 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
42 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
43 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()); }
44 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
45 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
46 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(); }
47 Reading files � Have to be careful. Suppose a file contains the line: � 34 567
48 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();
49 Reading files Scanner scanner = new Scanner(“file.txt”); Treats the String “file.txt” as the source, NOT the file “file.txt”
50 Writing Files � Use the PrintWriter class
51 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
52 The PrintWriter class � Methods: Writes a String � void print(String) :
53 The PrintWriter class � Methods: Writes a String � void print(String) : � void print(int) : Writes an int Writes a float � void print(float) :
54 The PrintWriter class � Methods: Writes a String � void print(String) : � void print(int) : Writes an int Writes a float � void print(float) : � void println(String) : Writes a String but also adds a line separator
Recommend
More recommend