Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working with files.

Similar presentations


Presentation on theme: "Working with files."— 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 DCS – 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) DCS – 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… DCS – 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(); DCS – SWC

6 Text files and Java FileReader reader = new FileReader(”input.txt”);
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 DCS – 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); DCS – SWC

8 Text files and Java When providing a file path as a constant string, the string must 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 DCS – 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 DCS – 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”); DCS – 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 DCS – SWC

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

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 DCS – SWC

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 DCS – SWC

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 DCS – SWC

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(); DCS – SWC

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(); DCS – SWC

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 DCS – SWC

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 5 1 2 9 DCS – SWC

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(); DCS – SWC

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 DCS – SWC

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… DCS – SWC

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 DCS – SWC

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 DCS – SWC

25 Object streams public interface Serializable { // Empty... } DCS – SWC

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 in-memory data to a file DCS – SWC

27 Object streams This is very convenient, but BE AWARE of a common ”trap” in serialization! To serialize an object, all fields in the object must themselves be serializable! DCS – SWC

28 Object streams Example: We want to make ItemCatalog serializable
An ItemCatalog class contains an… ArrayList<Item> instance field, which contains several… Item objects We want to make ItemCatalog serializable DCS – SWC

29 Object streams We must then mark the ItemCatalog class as being serializable ArrayList is itself serializable, so all is good… …BUT we must also mark the Item class itself as being serializable! Otherwise, an IOException will be thrown DCS – SWC

30 Object streams public class Bank implements Serializable {...}
Bank myBank = new Bank(); // Enter data into myBank object FileOutputStream fos = new FileOutputStream(”bank.dat”); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(myBank); oos.close(); fos.close(); DCS – SWC

31 Object streams public class Bank implements Serializable {...}
Bank myBank = new Bank(); // Reading Bank data from a file FileInputStream fis = new FileInputStream(”bank.dat”); ObjectInputStream ois = new ObjectInputStream(fis); myBank = (Bank)ois.readObject(); ois.close(); fis.close(); DCS – SWC


Download ppt "Working with files."

Similar presentations


Ads by Google