Download presentation
Presentation is loading. Please wait.
Published byMadlyn Bridges Modified over 9 years ago
1
Working with files
2
RHS – SOC 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
3
RHS – SOC 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)
4
RHS – SOC 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…
5
RHS – SOC 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();
6
RHS – SOC 6 Text files and Java FileReader reader = new FileReader(”input.txt”); Scanner in = new Scanner(reader); String line = in.nextLine(); Constructor takes file name as a String (remember ””) FileReader itself cannot be used for reading – need a Scanner object Several next… methods available next() nextInt() nextDouble() …
7
RHS – SOC 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);
8
RHS – SOC 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
9
RHS – SOC 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
10
RHS – SOC 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”);
11
RHS – SOC 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
12
RHS – SOC 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
13
RHS – SOC 13 Exercises Self-check: -- Review: R11.1, R11.2 Programming: P11.1, P11.7
14
RHS – SOC 14 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
15
RHS – SOC 15 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
16
RHS – SOC 16 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
17
RHS – SOC 17 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();
18
RHS – SOC 18 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();
19
RHS – SOC 19 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
20
RHS – SOC 20 Binary files – random access A file also has a file pointer The file pointer indicates where to read/write the next time 197705129
21
RHS – SOC 21 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();
22
RHS – SOC 22 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
23
RHS – SOC 23 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…
24
RHS – SOC 24 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
25
RHS – SOC 25 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
26
RHS – SOC 26 Object streams public interface Serializable { // Empty... }
27
RHS – SOC 27 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
28
RHS – SOC 28 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();
29
RHS – SOC 29 Exercises Self-check: 1, 6, 7, 8 Review: R19.3, R19.5, R19.7 Programming: –The Car Rental Project
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.