Presentation is loading. Please wait.

Presentation is loading. Please wait.

File class File myFile=new File(“c:/javaDemo/aa

Similar presentations


Presentation on theme: "File class File myFile=new File(“c:/javaDemo/aa"— Presentation transcript:

1 File class File myFile=new File(“c:/javaDemo/aa
File class File 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 objects exists(). canRead(). isDirectory()
Testing and Checking File objects exists() canRead() isDirectory() canWrite() isFile() equals() isHidden() if ( !( myFile.exists()) ) System.out.println(“File does not exist”);

3 Accessing File objects getName(). String name without path. getPath()
Accessing File objects getName() 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: null listFiles()  same as list() but with files members only

4 Accessing File objects Cont… lastModified()  long milliseconds since midnight on st of January 1970 Example: import java.util.Date; new Date(myDir.lastModified()) Thu Nov 27 10:58:51 EST 2008

5 Modifying File objects renameTo(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’, e- variable name> } // Multiple catch statements are allowed finally { <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
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.parseInt (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
BufferedWriter bw; bw=new BufferedWriter(new FileWriter (“a.out”)); newLine() write(String str) close() example: bw.write(“garbage out”); bw.newLine() bw.close()


Download ppt "File class File myFile=new File(“c:/javaDemo/aa"

Similar presentations


Ads by Google