1 / 66

COP 3503 FALL 2012 Shayan Javed Lecture 12

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.

hei
Download Presentation

COP 3503 FALL 2012 Shayan Javed Lecture 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP 3503 FALL 2012ShayanJavedLecture 12 Programming Fundamentals using Java

  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 ProgramNameparameter1 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: • booleanhasNext() : If scanner has more tokens

  20. The Scanner class • Methods: • booleanhasNext() : If scanner has more tokens • String next() : Returns the next String • intnextInt() : Returns the next int • doublenextDouble() : Returns the next double

  21. The Scanner class • Methods: • booleanhasNext() : If scanner has more tokens • String next() : Returns the next String • intnextInt() : Returns the next int • doublenextDouble() : Returns the next double • voiduseDelimiter(pattern: String) : Set’s the delimiting pattern (“ “ by default)

  22. The Scanner class • Methods: • booleanhasNext() : If scanner has more tokens • String next() : Returns the next String • intnextInt() : Returns the next int • doublenextDouble() : Returns the next double • voiduseDelimiter(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 importjava.util.Scanner; Scannerscanner = newScanner(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: • File(pathname: String): Creates a File object for the specified pathname. pathname = directory or file

  31. 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. The File class • Methods: • boolean exists() : If the file exists

  33. The File class • Methods: • boolean exists() : If the file exists • booleancanRead() : If the file exists and we can read it • booleancanWrite() : If the file exists and we can write to it

  34. The File class • Methods: • boolean exists() : If the file exists • booleancanRead() : If the file exists and we can read it • booleancanWrite() : 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. The File class • Methods: • boolean exists() : If the file exists • booleancanRead() : If the file exists and we can read it • booleancanWrite() : 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. The File class • Methods: • boolean exists() : If the file exists • booleancanRead() : If the file exists and we can read it • booleancanWrite() : 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. 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 = newScanner(newFile(“test.txt”)); while (input.hasNext()) { System.out.println(input.next()); }

  44. Reading Files // Reads in the file and outputs all the tokens Scanner input = newScanner(newFile(“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 = newScanner(newFile(“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 = newScanner(newFile(“test.txt”)); while (input.hasNext()) { System.out.println(input.next()); } } catch (FileNotFoundExceptionfe) { 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 = newScanner(newFile(“test.txt”)); intintValue = in.nextInt(); String line = in.nextLine();

  49. Reading files Scannerscanner = newScanner(“file.txt”); Treats the String “file.txt” as the source, NOT the file “file.txt”

  50. Writing Files • Use the PrintWriter class

More Related