1 / 12

Working with files

Working with files. Motivation. All our programs so far have only worked with data stored in primary storage (RAM) Data is lost when program is terminated Almost all ”real” programs allow us to save the state – by some definition – of the program to permanent storage (e.g disk)

nani
Download Presentation

Working with files

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. Working with files

  2. Motivation • All our programs so far have only worked with data stored in primary storage (RAM) • Data is lost when program is terminated • Almost all ”real” programs allow us to save the state – by some definition – of the program to permanent storage (e.g disk) • Such permanent storage is often a file RHS – SWC

  3. What is a file? • A collection of bytes, stored on a perma-nent storage media • Bytes can be interpreted as text, numerics, picture, etc. • Files have a name, and a position (path) • Myschedule.doc (name) • C:\documents\per\rhs (path) RHS – SWC

  4. Text files • A text file just contains characters (bytes inter-preted as characters) • Usually named .txt • Created with simple text editor like Notepad, or NetBeans… RHS – SWC

  5. Text files and Java • It is rather easy to read a text file in Java – we use the FileReader class and the Scanner class FileReader reader = new FileReader(”input.txt”); Scanner in = new Scanner(reader); String line = in.nextLine(); RHS – SWC

  6. Text files and Java Constructor takes file name as a String (remember ””) FileReader reader = new FileReader(”input.txt”); Scanner in = new Scanner(reader); String line = in.nextLine(); Several next… methods available next() nextInt() nextDouble() … FileReader itself cannot be used for reading – need a Scanner object RHS – SWC

  7. Text files and Java FileReader reader = new FileReader(”input.txt”); Scanner in = new Scanner(reader); int lineCount = 0; while (in.hasNextLine()) { String line = in.nextLine(); lineCount++; } in.close(); System.out.println(”Lines in file: ” + lineCount); RHS – SWC

  8. Text files and Java • When providing a file path as a constant string, the string mus look like this: ”c:\\documents\\psl\\work.txt”; • Notice the use of double backslash! • Recall that \ is used for escape characters! • We do not need double backslash when e.g the user types the path in a dialog RHS – SWC

  9. Text files and Java • It is also easy to write to a file in Java – we use the PrintWriter class PrintWriter out = new PrintWriter(”output.txt”); • That’s it – we can now use the well-known print(…) and println(…) methods RHS – SWC

  10. Text files and Java PrintWriter out = new PrintWriter(”output.txt”); for (int i = 0; i < 10; i++) { out.println(”Writing to a file is easy!”); } out.close(); System.out.println(”Done writing to file”); RHS – SWC

  11. Text files and Exceptions • Working with files is easy, but… • …errors beyond the control of the pro-grammer can happen! • File was not found • File was not a text file • File was different format than expected • …and so on RHS – SWC

  12. Text files and Exceptions • Code for handling files should include proper handling of exceptions! • IOException class, and subclass thereof • We can define more specific classes as well RHS – SWC

More Related