Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java File I/O


Published on

A presentation on Java File I/O for my ICS3U Computer Science class

Published in: Education, Technology
  • Be the first to comment

Java File I/O

  1. 1. File Input/Output (I/O)‏
  2. 2. File I/O <ul><li>Declare a file object </li></ul><ul><li>File myFile = new File(&quot;billy.txt&quot;); </li></ul><ul><li>a file object whose name is &quot;billy.txt” </li></ul>
  3. 3. File myFile = new File(&quot;billy.txt&quot;); <ul><li>The data type of myFile is… </li></ul><ul><li>… File </li></ul><ul><li>What kind of data type is this? </li></ul><ul><li>An abstract data type. (vs. concrete)‏ </li></ul>
  4. 4. Create a New File <ul><li>createNewFile() method </li></ul><ul><li>myFile.createNewFile(); // this creates // the file </li></ul>
  5. 5. When file creation goes horribly, horribly wrong <ul><li>If the file can’t be created an exception is thrown . </li></ul><ul><li>IOException - means there was an I/O failure. </li></ul><ul><li>It’s an unchecked exception </li></ul><ul><li>Compiler: write some special code to handle the exception. </li></ul>
  6. 6. Exception Handlers <ul><li>try…catch block </li></ul><ul><li>try { </li></ul><ul><li>myFile.createNewFile(); </li></ul><ul><li>System.out.println(&quot;file created&quot;); </li></ul><ul><li>} </li></ul><ul><li>catch ( e) {//can call ‘e’ </li></ul><ul><li>//anything </li></ul><ul><li>System.out.println(”Oops!&quot;); </li></ul><ul><li>} </li></ul>
  7. 7. try…catch block <ul><li>try block: contains code you’re trying to execute and that might throw the exception. </li></ul><ul><li>catch block: contains code that is executed when the exception is caught. </li></ul>
  8. 8. You Try <ul><li>Declare a file object </li></ul><ul><li>Create the file </li></ul><ul><li>Where do you suppose your program created the file? </li></ul><ul><li>Use WinXp’s search function if you can’t locate the file yourself. </li></ul>
  9. 9. throws – the keyword <ul><li>Guess what? </li></ul><ul><li>You don't absolutely need a try...catch block. </li></ul><ul><li>You can use the throws keyword instead. </li></ul><ul><li>It's simpler. </li></ul><ul><li>But less elegant. </li></ul>
  10. 10. Using throw <ul><li>public static void main(String[] args) throws <exceptionType> </li></ul><ul><li>e.g. </li></ul><ul><li>public static void main (String[] args) throws IOException, FileNotFoundException </li></ul><ul><li>It’s simpler than try…catch but… </li></ul><ul><li>Your program will crash when the exception occurs rather than do something elegant specified by the catch block. </li></ul>
  11. 11. Deleting Files <ul><li>We use the delete() method. </li></ul><ul><li>E.g. myFile.delete(); </li></ul><ul><li>The delete() method returns true if successful and false if not. </li></ul><ul><li>Since we’re interacting with the OS, we should check for success. </li></ul><ul><li>if(myFile.delete()) </li></ul><ul><li>//OK. File was deleted. Continue processing. </li></ul><ul><li>else </li></ul><ul><li>System.out.println(“ERROR: File deletion failed.”); </li></ul>
  12. 12. Reading from a file <ul><li>We need a way to connect our program to the file we want to read. </li></ul><ul><li>We use a stream . </li></ul>
  13. 13. Reading From A File: Creating a stream <ul><li>We need two things for a (useful) stream: </li></ul><ul><li>a FileReader object and </li></ul><ul><li>a BufferedReader object </li></ul>
  14. 14. Reading From A File: Creating a stream <ul><li>Create a FileReader object to use: </li></ul><ul><li>FileReader myFileRdr = new FileReader(“billy.txt”); </li></ul><ul><li>This presumes that “ billy.txt ” already exists. </li></ul><ul><li>If not? </li></ul><ul><li>An exception! </li></ul><ul><li>FileNotFoundException (see slide 10) </li></ul>
  15. 15. Reading From A File: BufferedReader <ul><li>FileReader is good creating a stream to connect to a file but not much else. </li></ul><ul><li>BufferedReader is good at reading from a stream. </li></ul><ul><li>BufferedReader myBuffRdr = new BufferedReader (myFileRdr); </li></ul>
  16. 16. Reading From a File: BufferedReader <ul><li>BufferedReader needs to know what kind of streaming object it’s going to read from when it’s created. </li></ul><ul><li>That’s why we pass the FileReader object ( myFileRdr ) to it when we create it. </li></ul><ul><li>BufferedReader myBuffRdr = new BufferedReader (myFileRdr); </li></ul>
  17. 17. Reading from a File <ul><li>FileReader myFileRdr = new FileReader(“billy.txt); </li></ul><ul><li>BufferedReader myBuffRdr = new BufferedReader(myFileRdr); </li></ul><ul><li>myBuffRdr reads from the myFileRdr stream which is connected to billy.txt . </li></ul>
  18. 18. Using BufferedReader <ul><li>BufferedReader implements our old friend… </li></ul><ul><li>… readLine() </li></ul><ul><li>myBuffRdr.readLine() reads a line of text from the file that was pointed to by the FileReader object. </li></ul><ul><li>i.e. we read a line from billy.txt </li></ul>
  19. 19. Using BufferedReader <ul><li>myBuffRdr.readLine() returns a string…(as usual) </li></ul><ul><li>String fileLine; </li></ul><ul><li>fileLine = myBuffRdr.readLine(); </li></ul>
  20. 20. Reading until the End of the File (EOF) <ul><li>String fileLine; </li></ul><ul><li>while ((fileLine = myBuffRdr.readLine()) != null) </li></ul><ul><li>System.out.println(fileLine); </li></ul><ul><li>At the end of the file ( EOF ), readLine() returns a special value called null instead of a string. </li></ul><ul><li>Therefore, the loop runs, reading lines from the file, until readLine() returns null . </li></ul><ul><li>null means we’re at the end of the file and should stop reading. </li></ul>
  21. 21. You try <ul><li>Follow the handout instructions to write a program called cat that puts all this together. </li></ul>
  22. 22. Parsing/Splitting the Input String into words <ul><li>We need to be able to split the input line into individual words. </li></ul><ul><li>We use the String method called S plit() to do this. </li></ul>
  23. 23. Parsing/Splitting the Input String into words <ul><li>String fileLine = myBuffRdr.readLine(); </li></ul><ul><li>String[] words; </li></ul><ul><li>words = fileLine. Split(new char[]{‘:‘}); </li></ul><ul><li>This gives us an array of Strings . </li></ul><ul><li>Each element of the array is one word from the line that we read from the file. </li></ul>
  24. 24. Print one word per line <ul><li>//words = fileLine. //Split(new char[]{‘:‘}); </li></ul><ul><li>While((fileLine=myBuffRdr.readLine()) != null) { </li></ul><ul><li>//assumes “:” is the field delimeter </li></ul><ul><li>words = fileLine.Split(new char[]{‘:‘}); </li></ul><ul><li>for (int i = 0; i<words.length; i++) </li></ul><ul><li>System.out.println(words[i]); </li></ul>
  25. 25.
  26. 26. Writing Program Output to a File <ul><li>Very similar to reading from a file. </li></ul><ul><li>Declare a FileWriter object which will connect to the file we want to write to. </li></ul><ul><li>FileWriter myFileWrtr = new FileWriter(&quot;Billy.txt&quot;); </li></ul><ul><li>What do you suppose &quot; Billy.txt &quot; refers to? </li></ul>
  27. 27. Writing to a File <ul><li>Yup. It's the name of the file we want to write to. </li></ul><ul><li>If the file doesn't exist, declaring the FileWriter object will create it. </li></ul><ul><li>If the file does already exist, declaring the FileWriter object will overwrite it. </li></ul>
  28. 28. Writing to a File - BufferedWriter <ul><li>You also need a BufferedWriter (just like you needed a BufferedReader ). </li></ul><ul><li>BufferedWriter myBuffWrter = new BufferedWriter(myFWrtr); </li></ul>
  29. 29. Writing to a file - write() <ul><li>BufferedWriter has a write() method to use for writing a string to the file. </li></ul><ul><li>myBuffWrtr.write(”Billy is a bad boy.&quot;); </li></ul><ul><li>myBuffWrtr.write(”Bobby is too.&quot;); </li></ul>
  30. 30. Writing to a file - write() <ul><li>There’s a problem with the two previous lines of code. </li></ul><ul><li>They produce outpout like this: </li></ul><ul><li>Billy is a bad boy.Bobby is too. </li></ul><ul><li>We need to use the newLine() method. </li></ul><ul><li>myBuffWrtr.newLine(); </li></ul>
  31. 31. Writing to a file - newLine () <ul><li>myBuffWrtr.write(”Billy is a bad boy.&quot;); </li></ul><ul><li>myBuffWrtr.newLine(); </li></ul><ul><li>myBuffWrtr.write(”Bobby is too.&quot;); </li></ul><ul><li>Will produce: </li></ul><ul><li>Billy is a bad boy. </li></ul><ul><li>Bobby is too. </li></ul>
  32. 32. Writing to a file - close() <ul><li>When we’re done with the file we should tell the operating system to close it: </li></ul><ul><li>myBuffWrtr.close(); </li></ul>