120 likes | 243 Views
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)
E N D
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
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
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
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
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
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
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
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
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
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
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