140 likes | 223 Views
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
E N D
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 File I/O and Streams(Part I) Course Lecture Slides12thJuly 2010 GaneshViswanathan
The File Class • java.io package • Provides for system input and output through data streams, serialization and the file system.
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())); }
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
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
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(); } }
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
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();
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(); } }
Caution Scanner scanner = new Scanner(“file.txt”); is treating the String “file.txt” as source, NOT the file “file.txt”
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