Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1."— Presentation transcript:

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

2 2 / 65 Input/Output (IO)

3 3 / 65 I/O  So far we have looked at modeling classes

4 4 / 65 I/O  So far we have looked at modeling classes  Not much in the way of Input...

5 5 / 65 Input  3 ways of providing input to the program:

6 6 / 65 Input  3 ways of providing input to the program:  Pass parameters directly to the program

7 7 / 65 Input  3 ways of providing input to the program:  Pass parameters directly to the program  Command-line input from the user

8 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

9 9 / 65 Passing parameters  When running the program can directly pass parameters to it

10 10 / 65 Passing parameters  When running the program can directly pass parameters to it java ProgramName parameter1 parameter2...

11 11 / 65 Passing parameters public static void main(String[] args) { // args is the array of all parameters // args[0] would be the first parameter }

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

13 13 / 65 Command-line input  Receive input from the console during execution

14 14 / 65 Command-line input  Receive input from the console during execution  Use the Scanner class

15 15 / 65 The Scanner class  Used for reading data

16 16 / 65 The Scanner class  Used for reading data  Constructors:  Scanner(InputStream) InputStream = System.in

17 17 / 65 The Scanner class  Used for reading data  Constructors:  Scanner(InputStream) InputStream = System.in  Scanner(String)

18 18 / 65 The Scanner class  Used for reading data  Constructors:  Scanner(InputStream) InputStream = System.in  Scanner(String)  Scanner(File)

19 19 / 65 The Scanner class  Methods:  boolean hasNext() : If scanner has more tokens

20 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

21 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)

22 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

23 23 / 65 Command-line input  Use the “next..” methods to read from the standard input

24 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));

25 25 / 65 File Input  Ability to read files essential to any language.

26 26 / 65 File Input  Ability to read files essential to any language.  Two ways to store data:  Text format:

27 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

28 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

29 29 / 65 The File class  java.io package  Represents a “file” object  Used for input/output through data streams, the file system and serialization.

30 30 / 65 The File class  Constructors:  File(pathname: String):Creates a File object for the specified pathname. pathname = directory or file

31 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 child under the directory parent. child may be a filename or subdirectory.

32 32 / 65 The File class  Methods:  boolean exists() : If the file exists

33 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

34 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

35 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

36 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

37 37 / 65 Reading Files  Use the Scanner class  new Scanner(File)

38 38 / 65 Reading Files  How does Scanner really work?

39 39 / 65 Reading Files  How does Scanner really work?  Breaks file contents into tokens  Uses a delimiter

40 40 / 65 Reading Files  How does Scanner really work?  Breaks file contents into tokens  Uses a delimiter  Delimiter by default is whitespace

41 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

42 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

43 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()); }

44 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

45 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

46 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(); }

47 47 / 65 Reading files  Have to be careful. Suppose a file contains the line:  34 567

48 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();

49 49 / 65 Reading files Scanner scanner = new Scanner(“file.txt”); Treats the String “file.txt” as the source, NOT the file “file.txt”

50 50 / 65 Writing Files  Use the PrintWriter class

51 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

52 52 / 65 The PrintWriter class  Methods:  void print(String) : Writes a String

53 53 / 65 The PrintWriter class  Methods:  void print(String) : Writes a String  void print(int) : Writes an int  void print(float) : Writes a float

54 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

55 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

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

57 57 / 65 Writing Files PrintWriter 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(); }

58 58 / 65 Writing Files  Problem: What if you want to append to the file not replace it?

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

60 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

61 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

62 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)

63 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) )

64 64 / 65 Writing Files PrintWriter 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(); }

65 65 / 65 Writing Files – Append PrintWriter 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(); }

66 66 / 65 Summary  Use Scanner for reading from command-line and files.  Based on delimiters  Use PrintWriter for writing to files


Download ppt "1 / 65 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 12 Programming Fundamentals using Java 1."

Similar presentations


Ads by Google