Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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) • Such permanent storage is often a file RHS – SOC 2 What is a file? • A collection of bytes, stored on a permanent 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 – SOC 3 Text files • A text file just contains characters (bytes interpreted as characters) • Usually named .txt • Created with simple text editor like Notepad, or NetBeans… RHS – SOC 4 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 – SOC 5 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 FileReader itself cannot be used for reading – need a Scanner object next() nextInt() nextDouble() … RHS – SOC 6 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 – SOC 7 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 – SOC 8 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 wellknown print(…) and println(…) methods RHS – SOC 9 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 – SOC 10 Text files and Exceptions • Working with files is easy, but… • …errors beyond the control of the programmer can happen! – File was not found – File was not a text file – File was different format than expected – …and so on RHS – SOC 11 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 – SOC 12 Exercises • Self-check: -• Review: R11.1, R11.2 • Programming: P11.1, P11.7 RHS – SOC 13 Binary files • A binary file contains ”raw” bytes that can be interpreted as almost anything • Can have any extension • Can be created from various sources RHS – SOC 14 Binary files • Note that a binary file does usually not contain information about itself (metadata) • We must know the meaning of the file in advance – Picture – Object data – Encrypted data RHS – SOC 15 Binary files and Java • In Java, a binary file is considered a ”stream” of bytes • We generally use InputStream and OutputStream to handle binary data • For binary files, we use: – Read: FileInputStream – Write: FileOutputStream RHS – SOC 16 Binary files and Java FileInputStream inS = new FileInputStream(”input.dat”); boolean done = false; while (!done) { int next = inS.read(); byte nextByte; if (next != -1) { nextByte = (byte)next; ... // Do something with nextByte } else {done = true; } } inS.close(); RHS – SOC 17 Binary files and Java FileOutputStream outS = new FileOutputStream(”output.dat”); byte nextByte; boolean done = false; while (!done) { ... // We get bytes to write from somewhere outS.write(nextByte); } outS.close(); RHS – SOC 18 Binary files – random access • Sequential processing of binary files is fairly simple – but possibly inefficient • What if a binary file contained data for thousands of bank accounts, and we just needed data for one…? • Java also allows ”random access” to data in a binary file RHS – SOC 19 Binary files – random access • A file also has a file pointer • The file pointer indicates where to read/write the next time 1 9 7 7 0 5 RHS – SOC 1 2 9 20 Binary files – random access • Random access in a file is done using the RandomFileAccess class – – – – new RandomAccessFile(”data.bin”,”rw”) f.seek(pointerPos) int pointerPos = f.getFilePointer() long fileLen = f.length(); RHS – SOC 21 Binary files – random access • Random access can be efficient when dealing with large binary files • Again, no help from Java – you must decide and obey a data format yourself • If you overwrite data – too bad… • A little bit of help – Java does offer methods for read/write of numeric types from a binary file RHS – SOC 22 Binary files and Java • Java only provides simple methods for reading/writing binary files – what else could it do…? • We are responsible for interpreting and processing the stream of bytes • Java gets the bytes for us, it does not try to analyse them… RHS – SOC 23 Object streams • Wouldn’t it be nice, if we could – Convert an object to binary format – Write the binary data to a file – Close the program – Open the program anew – Read the binary data from the file – Convert the data back to an object RHS – SOC 24 Object streams • An object stream can do just that! • Two classes available – ObjectOutputStream – ObjectInputStream • Only prerequisite; a class must implement the Serializable interface, in order to be writable and readable by an object stream RHS – SOC 25 Object streams public interface Serializable { // Empty... } RHS – SOC 26 Object streams • Typical way to use object streams: – Define a class where one object can contain all relevant data (for instance a Bank class) – Let the class implement Serializable – Use the class for writing/reading data • This is extremely useful for saving inmemory data to a file RHS – SOC 27 Object streams public class Bank implements Serializable {...} // Writing Bank data to a file Bank myBank = new Bank(); ... // Enter data into myBank object FileOutputStream fos = new FileOutputStream(”bankA.dat”); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(myBank); // Reading Bank data from a file FileInputStream fis = new FileInputStream(”bankB.dat”); ObjectInputStream ois = new ObjectInputStream(fis); myBank = (Bank)ois.readObject(); RHS – SOC 28 Exercises • Self-check: 1, 6, 7, 8 • Review: R19.3, R19.5, R19.7 • Programming: – The Car Rental Project RHS – SOC 29