160 likes | 246 Views
File class File myFile=new File(“c:/javaDemo/aa.txt”) or File myDir=new File(“c:/javaDemo”) File myFile=new File(myDir,“aa.txt”).
E N D
File classFile myFile=new File(“c:/javaDemo/aa.txt”)or File myDir=new File(“c:/javaDemo”) File myFile=new File(myDir,“aa.txt”)
Testing and Checking File objectsexists() canRead() isDirectory() canWrite() isFile() equals() isHidden() if ( !( myFile.exists()) ) System.out.println(“File does not exist”);
Accessing File objectsgetName() String name without path getPath() String path getAbsolutePath() String absolute path list() if Dir: String array with list if empty Dir: empty String array if file: nulllistFiles() same as list() but with files members only
Accessing File objects Cont…lastModified() long milliseconds since midnight on 1st of January 1970Example:import java.util.Date;new Date(myDir.lastModified())Thu Nov 27 10:58:51 EST 2008
Modifying File objectsrenameTo(File path) mkdir() createNewFile()delete()(will not deleted not-empty directory)deleteOnExit() (when the program ends)
Exceptions The File class method createNewFile() will throw a IOException the file cannot be created. To prevent this from aborting your program if this exception occurs, it needs to be handled.
Try-Catch blocks try { <statements which may potentially cause exceptions> } catch (<exceptionType> e) { <What to do in case of exception ‘e’> } // Multiple catch statements are allowed finally (<exceptionType> e) { <What to do before leaving the try-catch block> }
The File Stream Allows for sequential file access. Separate classes used for input streams and output streams. Text which appears in a text file as: This is My text. Would appear like this in a file stream:
The FileReader Class The FileReader class serves as the input file stream. It uses the following methods: FileReader(File fileName) The FileReader Constructor. It will throw a FileNotFoundException if the file does not exist. close() Closes the file stream. Throws an IOExpection if the file cannot be closed.
The BufferedReader Class The BufferedReader class performs the actual reading from the FileReader. It uses the constructor: BufferedReader(Reader stream) “Reader” is the superclass of FileReader
BufferedReader br;br=new BufferedReader(new FileReader (“a.in”));read()readLine()close()while( (str = br.readLine() ) != null){ …….}
File myFile = new File(“inputFile.txt”); FileReader fr; BufferedReader reader; String lineIn; try { fr = new FileReader(myFile); reader = new BufferedReader(fr); while ((lineIn = reader.readLine()) != null) { System.out.println(lineIn); } reader.close(); fr.close(); } catch (FileNotFoundException e) {} catch (IOException e) {}
Processing Numeric Data Data read in with BufferedReaders is interpreted as Strings. If this data should be interpreted as a numeric, you will have to use methods from the Double or Integer classes. For Doubles: Double.parseDouble(String inputString) For Integers: Integer.parseInteger(String inputString)
The FileWriter Class The FileWriter class serves as the output file stream. It uses the following methods: FileWriter(File fileName, boolean append) The FileWriter Constructor. If append is true, it will add to the end of the existing file if it exists. If it is false, it will overwrite the file if it exists. It will throw an IOException if the file cannot be created or opened. close() Closes the file stream. Throws an IOExpection if the file cannot be closed.
The BufferedWriter Class The BufferedWriter class performs the actual reading from the FileWriter. It uses the following constructor: BufferedReader(Writer stream) “Writer” is the FileWriter superclass
BufferedWriter bw;bw=new BufferedWriter(new FileWriter (“a.out”));newLine()write(String str)close()example: bw.write(“garbage out”);bw.newLine()bw.close()