Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Overview Examples Tokenizing File input/output: Almost identical console input/output Prof. Dionne Aleman MIE250: Fundamentals of object-oriented programming University of Toronto MIE250: Fundamentals of object-oriented programming (Aleman) Overview File I/O Examples 1 / 11 Tokenizing File handling I File handling refers to reading in and writing to files (file I/O). I Java is notable for not having simple ways to do I/O, whether from/to console or file. I Lots of options available for I/O ... we will use BufferedReader. MIE250: Fundamentals of object-oriented programming (Aleman) Overview File I/O Examples 2 / 11 Tokenizing Similarity to reading from the console 1 2 // object to read from screen BufferedReader cin = new BufferedReader ( new InputStreamReader ( System . in )); 3 4 5 // object to read from file BufferedReader fin = new BufferedReader ( new FileReader ( filename )); 6 7 8 // read from screen String line1 = cin . readLine () ; 9 10 11 // read from file String line2 = fin . readLine () ; MIE250: Fundamentals of object-oriented programming (Aleman) File I/O 3 / 11 Overview Examples Tokenizing Similarity to writing to the console 1 2 // write to screen System . out . println ( strSomeText ) ; 3 4 5 // object to write file BufferedWriter fout = new BufferedWriter ( new FileWriter ( filename )); 6 7 8 9 10 // write to file fout . write (" This is the first line in my file . " ) ; fout . newLine () ; fout . write (" This is the second line in my file . " ) ; 11 12 13 14 // flush the buffer and close the file ( IMPORTANT !) fout . flush () ; fout . close () ; MIE250: Fundamentals of object-oriented programming (Aleman) Overview File I/O Examples 4 / 11 Tokenizing File I/O notes I Exceptions that must be handled: I I IOException FileNotFoundException I Opening a file to write erases the file. I Opening a file to write creates the file if the file doesn’t exist. MIE250: Fundamentals of object-oriented programming (Aleman) Overview File I/O Examples 5 / 11 Tokenizing Creating a file and then printing it out 1 import java . io .*; 2 3 public class FileIO { 4 public static void main ( String [] args ) throws IOException { int i = 1; String line ; BufferedWriter fout = new BufferedWriter ( new FileWriter (" test . dat ")); do { fout . write ( i + " " + i * i ) ; fout . newLine () ; } while ( i ++ < 5) ; fout . flush () ; fout . close () ; 5 6 7 8 9 10 11 12 13 14 15 BufferedReader fin = new BufferedReader ( new FileReader (" test . dat ")); do { line = fin . readLine () ; if ( line != null ) System . out . println ( line ) ; } while ( line != null ) ; fin . close () ; 16 17 18 19 20 21 22 } 23 24 } src/FileIO.java MIE250: Fundamentals of object-oriented programming (Aleman) File I/O 6 / 11 Overview Examples Tokenizing Checking if a file or directory exists 1 import java . io .*; 2 3 4 public class FileExists { public static void main ( String [] args ) throws IOException { 5 BufferedReader cin = new BufferedReader ( new InputStreamReader ( System . in )) ; System . out . print (" Enter a file name or directory to check : "); String filename = cin . readLine () ; 6 7 8 9 File file = new File ( filename ) ; 10 11 boolean exists = file . exists () ; if (! exists ) { System . out . println ( filename + " does not exist ! " ) ; } else { System . out . println ( filename + " exists ! " ) ; } 12 13 14 15 16 17 18 } 19 20 } src/FileExists.java MIE250: Fundamentals of object-oriented programming (Aleman) Overview File I/O Examples 7 / 11 Tokenizing Some useful File methods Method Description canRead() Check if the file is readable canWrite() Check if the file is writable delete() Delete the file/dir deleteOnExit() Delete the file/dir when the program quits exists() Check if the file/dir exists isDirectory() Check if the file/dir is a directory isFile() Check if the file/dir is a file listFiles() List all the files in the directory MIE250: Fundamentals of object-oriented programming (Aleman) Overview File I/O Examples 8 / 11 Tokenizing Tokenizing I fin.readLine() returns a String. I What if we want to store each value in a line separately? I Tokenizing a string is done using str.split(token); MIE250: Fundamentals of object-oriented programming (Aleman) File I/O 9 / 11 Overview Examples Tokenizing A tokenizing example Say we want to read this file and put the values into unique variables: 1 2 3 4 5 1 2 3 4 5 1 1 4 8 9 27 16 64 25 125 // need blank line at the end 6 MIE250: Fundamentals of object-oriented programming (Aleman) Overview 1 File I/O Examples 10 / 11 Tokenizing import java . io .*; 2 3 public class FileIOToken { 4 public static void main ( String [] args ) throws IOException { String line ; BufferedWriter fout = new BufferedWriter ( new FileWriter (" test . dat ")); for ( int i = 1; i <= 5; i ++) { fout . write ( i + " " + i * i + " " + i * i * i ) ; fout . newLine () ; } fout . flush () ; fout . close () ; 5 6 7 8 9 10 11 12 13 14 BufferedReader fin = new BufferedReader ( new FileReader (" test . dat ")); do { line = fin . readLine () ; if ( line != null ) { String [] splitString = line . split ( " " ) ; int i1 = Integer . parseInt ( splitString [0]) ; int i2 = Integer . parseInt ( splitString [1]) ; int i3 = Integer . parseInt ( splitString [2]) ; System . out . println ( i1 + " , " + i2 + " , " + i3 ) ; } } while ( line != null ) ; fin . close () ; 15 16 17 18 19 20 21 22 23 24 25 26 } 27 28 } src/FileIOToken.java MIE250: Fundamentals of object-oriented programming (Aleman) File I/O 11 / 11