Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.

Similar presentations


Presentation on theme: "Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion."— Presentation transcript:

1 Chapter 91 Streams and File I/O Chapter 9

2 2 Reminders Project 6 released: due Nov 03 @ 10:30 pm Project 4 regrades due by midnight tonight Discussion groups now twice a week (7-9 pm: M in CS G066, W in Phys 11)

3 Chapter 93 Exam 2 Tuesday, October 25 7:00 – 8:00 PM Physics 112 Covers chapters 5-9 (and material from 1-4 as well) Similar format as Exam 1 –20 MC –4 programming

4 Chapter 94 Why Use Files for I/O? Keyboard input and screen output deal only with temporary data, which is lost when the program ends. Files permit data to be stored permanently (or at least until a program changes the file). Input files can be used over and over by different programs. Files also provide convenient storage and retrieval of large quantities of data.

5 Chapter 95 Text Files and Binary Files All data in a file is stored as binary digits. –Files with contents that must be treated as sequences of binary digits are called binary files; binary files can be read only by machines.

6 Chapter 96 Text Files and Binary Files, cont. Sometimes, it is more convenient to think of a file’s contents as a sequence of characters. –Files with streams and methods to make them look like sequences of characters are called text files; text files can be read by people.

7 Chapter 97 Text Files and Binary Files, cont. However, all files are binary in the computer –Just a sequence of 0s and 1s: it’s how we (or programs) interpret those 0s and 1s

8 Chapter 98 Text File Output Example

9 Chapter 99 Use toString for Output

10 Chapter 910 Text File Input Example

11 Chapter 911 The StringTokenizer Class, cont. Tokens are nonwhitespace characters. example StringTokenizer tokenizer = new StringTokenizer(“Read my lips!”) while (tokenizer.hasMoreTokens()) { System.out.println (tokenizer.nextToken()); }

12 Chapter 912 The StringTokenizer Class, cont. This will produce Read my lips!

13 Chapter 913 The StringTokenizer Class, cont. Separators are whitespace characters unless otherwise specified. To specify a set of separators, a string consisting of all the separator characters is given as a second argument to the constructor. example … new StringTokenizer(“Read my lips!”, “\n.,!”);

14 Chapter 914 Some Methods in Class StringTokenizer constructors public StringTokenizer(String theString) public StringTokenizer(String theString, String delimiters) more tokens? public boolean hasMoreTokens() next token public String nextToken() remaining tokens public int countTokens()

15 Chapter 915 Example: StringTokenizer Display the words separated by any of the following characters: space, new line (\n), period (.) or comma (,). String inputLine = keyboard.nextLine(); StringTokenizer wordFinder = new StringTokenizer(inputLine, " \n.,"); while(wordFinder.hasMoreTokens()) { System.out.println(wordFinder.nextToken()); }

16 Chapter 916 Example: StringTokenizer String inputLine = keyboard.nextLine(); StringTokenizer wordFinder = new StringTokenizer(inputLine, " \n.,"); while(wordFinder.hasMoreTokens()) { System.out.println(wordFinder.nextToken()); } Question 2b or !tooBee Entering " Question,2b.or !tooBee. " gives this output:

17 Chapter 917 Java Tip: Testing for the End of a Text File When method readLine in class BufferedReader attempts to read beyond the end of a file, the method returns the value null. When method read attempts to read beyond the end of a file, the method returns the value -1.

18 Chapter 918 Using the File Class, cont.

19 Chapter 919 Binary Files Binary files store data in the same format used for main memory. Bytes in main memory and bytes in binary files are read similarly, which leads to efficiency. Binary files created by a Java program on one computer can be read by a Java program on a different computer.

20 Chapter 920 Binary Files, cont. Class ObjectInputStream and class ObjectOutputStream are used to process binary files. –Data is read or written, one byte at a time. –Numbers and characters are converted automatically to bytes for storage in a binary file. –Data in files can be treated as Java primitive data types, as strings, or as other objects.

21 Chapter 921 Opening a Binary File syntax ObjectOutputStream Output_Stream_Name = new ObjectOutputStream (new FileOutputStream(File_Name)); example ObjectOutputStream myOutputStream = new ObjectOutputStream (new FileOutputStream (“myfile.dat”));

22 Chapter 922 ObjectOutputStream

23 Chapter 923 Output to Binary Files Using ObjectOutputStream, cont. The numbers are not in human-readable form because there are no lines or other separators.

24 Chapter 924 Some Methods in Class ObjectOutputStream to create public ObjectOutputStream(OutputStream streamObject) to create a stream new ObjectOutputStream (new FileOutputStream (File_Name_or_File_Object)) to write a primitive type public void writeInt(int n) throws IOException

25 Chapter 925 Some Methods in Class ObjectOutputStream, cont. to write a primitive type, cont. public void writeLong(long n) throws IOException public void writeDouble(double x) throws IOException public void writeFloat(float x) throws IOException

26 Chapter 926 Some Methods in Class ObjectOutputStream, cont. public void writeChar(int n) throws IOException public void writeBoolean(boolean b) throws IOException to write a String public void writeUTF(String aString) throws IOException

27 Chapter 927 Some Methods in Class ObjectOutputStream, cont. There is no method writeString. Instead, use method writeUTF. UTF stands for Unicode Text Format. UTF provides short, efficient codes for ASCII characters.

28 Chapter 928 Some Methods in Class ObjectOutputStream, cont. To write an object public void writeObject(Object anObject) throws IOException, NotSerializableException, InvalidClassException to close public void close() throws IOException

29 Chapter 929 Different Types in the Same File Different types can be written to the same file. However, the different types must be read from the file just as they were written to the file.

30 Chapter 930 Reading Input from a Binary File Using ObjectInputStream A file written using ObjectOutputStream can be read using ObjectInputStream. The methods in class ObjectInputStream correspond to the methods in class ObjectOutputStream.

31 Chapter 931 Some Methods in Class ObjectInputStream to create ObjectInputStream (InputStream streamObject) to create a stream new ObjectInputStream (new FileInputStream (File_Name_or_File_Object) to read a primitive type public int readInt() throws IOException

32 Chapter 932 Some Methods in Class ObjectInputStream, cont. to read a primitive type, cont. public long readLong() throws IOException public double readDouble() throws IOException public float readFloat() throws IOException public char readChar() throws IOException public boolean ReadBoolean() throws IOException

33 Chapter 933 Some Methods in Class ObjectInputStream, cont. to read a String public String readUTF() throws IOException to read an object public Object readObject() throws ClassNotFoundException, InvalidClassException, OptionalDataException, IOException to close public void close() throws IOException

34 Chapter 934 Opening an Input File syntax ObjectInputStream Input_Stream_Name = new ObjectInputStream(new FileInputStream(File_Name)); example ObjectInputStream myInputStream = new ObjectInputStream(new FileInputStream(“myfile.dat”));

35 Chapter 935 The EOFException Class ObjectInputStream methods that read from a binary file throw an EOFException when they try to read beyond the end of the file. When using class ObjectInputStream, the class EOFException can test for the end of a file.

36 Chapter 936 The EOFException Class

37 Chapter 937 Checking for the End of File Different classes with file reading methods check for the end of a file in different ways. –Binary files throw an exception in the class EOFException. –A text file returns a special value, such as null. Be sure to test for the end of the file in the correct way.

38 Chapter 938 The Classes FileInputStream and FileOutputStream We used stream class FileInputStream when we created a stream of class ObjectInputStream. We used stream class FileOutputStream when we created a stream of class ObjectOutputStream.

39 Chapter 939 Binary I/O of Class Objects Using method writeObject of class ObjectOutputStream you can output class objects to a binary file, and then read objects from the file using method readObject of class ObjectInputStream. However, the class being written and read must be serializable.

40 Chapter 940 Binary I/O of Class Objects, cont. To make a class serializable, add implements Serializable to the class heading. example public class SomeClass implements Serializable The Serializable interface is available after importing java.io.*

41 Chapter 941 Binary I/O of Class Objects,

42 Chapter 942

43 Chapter 943 Files and toString Method toString provides convenient output to the screen and to a text file. However, method toString is not needed for object I/O to a binary file.

44 Chapter 944 The Serializable Interface A class which is serializable affects how Java performs file I/O with objects of the class. –Java assigns a serial number to each object of the class that it writes to a stream of type ObjectOutputStream. –If the object is written more than once, Java writes only the serial number for the object.

45 Chapter 945 Graphics Supplement

46 Chapter 946 Graphics Supplement, cont.

47 Chapter 947 Graphics Supplement, cont.

48 Chapter 948 Graphics Supplement, cont.

49 Chapter 949 Summary You have become familiar with the concept of an I/O stream. You now understand the difference between binary files and text files. You have learned how to save data in a file. You have learned how to read data from a file.

50 Chapter 950 Summary, cont. You have learned how use the classes ObjectOutputStream and ObjectInputStream to read and write class objects with binary files.


Download ppt "Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion."

Similar presentations


Ads by Google