1 / 14

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. File I/O and Streams (Part I ) . Course Lecture Slides 12 th July 2010. Ganesh Viswanathan. The File Class. java.io package

justis
Download Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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. CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 File I/O and Streams(Part I) Course Lecture Slides12thJuly 2010 GaneshViswanathan

  2. The File Class • java.io package • Provides for system input and output through data streams, serialization and the file system.

  3. The File Class

  4. Example public static void main(String[ ] args) { java.io.File file = new java.io.File("image/us.gif"); System.out.println("Does it exist? " + file.exists()); System.out.println("Can it be read? " + file.canRead()); System.out.println("Can it be written? " + file.canWrite()); System.out.println("Is it a directory? " + file.isDirectory()); System.out.println("Is it a file? " + file.isFile()); System.out.println("Is it absolute? " + file.isAbsolute()); System.out.println("Is it hidden? " + file.isHidden()); System.out.println("Absolute path is " + file.getAbsolutePath()); System.out.println("Last modified on " + new java.util.Date(file.lastModified())); }

  5. Two ways to store data • Text Format • Human-readable form • Can be read by text editors • Binary Format • can not be read by text editors

  6. Text I/O • Use PrintWriter class to write strings and numeric values to a text file • Use the Scanner class to read strings and numeric values from a text file

  7. Writing Data Using PrintWriter

  8. public static void main(String[ ] args) { java.io.PrintWriter output ; try { output = new java.io.PrintWriter(“scores.txt”); // 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(IOExceptionioe) { System.out.println(ioe.toString()); } finally { if (output != null) output.close(); } }

  9. Reading Data Using Scanner

  10. How does it work? • Scanner breaks the file contents into tokens using a delimiter pattern • By default the delimiter pattern is whitespace • Reads a token and convert it into the required type • Can change the delimiter by using the useDelimiter() method

  11. Few Important Details • Suppose test.txt contains a 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”)); intintValue = in.nextInt(); String line = in.nextLine();

  12. public static void main(String[ ] args) { try { java.io.File file = new java.io.File("scores.txt"); java.util.Scanner input = null; input = new java.util.Scanner(file); while (input.hasNext()) { String firstName = input.next(); String mi = input.next(); String lastName = input.next(); int score = input.nextInt(); System.out.println( firstName + " " + mi + " " + lastName + " " + score); } } catch (IOExceptionioe) { System.out.println(ioe.toString()); } finally { if (input != null) input.close(); } }

  13. Caution Scanner scanner = new Scanner(“file.txt”); is treating the String “file.txt” as source, NOT the file “file.txt”

  14. Get more info! • Java docs: java.io • http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/io/package-summary.html • Java docs: Scannerhttp://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Scanner.html

More Related