1 / 16

Exceptions

File class File myFile=new File(“c:/javaDemo/aa.txt”) or File myDir=new File(“c:/javaDemo”) File myFile=new File(myDir,“aa.txt”).

Download Presentation

Exceptions

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. File classFile myFile=new File(“c:/javaDemo/aa.txt”)or File myDir=new File(“c:/javaDemo”) File myFile=new File(myDir,“aa.txt”)

  2. Testing and Checking File objectsexists() canRead() isDirectory() canWrite() isFile() equals() isHidden() if ( !( myFile.exists()) ) System.out.println(“File does not exist”);

  3. 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

  4. 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

  5. Modifying File objectsrenameTo(File path) mkdir() createNewFile()delete()(will not deleted not-empty directory)deleteOnExit() (when the program ends)

  6. 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.

  7. 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> }

  8. 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:

  9. 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.

  10. 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

  11. BufferedReader br;br=new BufferedReader(new FileReader (“a.in”));read()readLine()close()while( (str = br.readLine() ) != null){ …….}

  12. 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) {}

  13. 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)

  14. 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.

  15. 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

  16. BufferedWriter bw;bw=new BufferedWriter(new FileWriter (“a.out”));newLine()write(String str)close()example: bw.write(“garbage out”);bw.newLine()bw.close()

More Related