Download I/O & Histogram Demo: Lecture Notes for CIS120 Programming Languages and Techniques and more Study notes Operating Systems in PDF only on Docsity!
Programming Languages
and Techniques
(CIS120)
Lecture 34
I/O & Histogram Demo
Chapter 28
Announcements
- HW7: Chat Server
- Due yesterday (soft deadline)
- HW 8: Game
- Due week from Wednesday (hard deadline)
- No late period, demo with your TA during reading days
- Final exam
- 9AM, Monday May 4 th through 11AM, Wednesday May 6 th
- PDF based, submit on Gradescope
- Practice exam (Fall 19 exam) available tomorrow
- Review session : 5-7PM, Thursday April 30 th on Zoom
I/O Streams
- The stream abstraction represents a communication channel with the outside world. - can be used to read or write a potentially unbounded number of data items (unlike a list) - data items are read from or written to a stream one at a time
- The Java I/O library uses subtyping to provide a unified view of disparate data sources and sinks.
Low-level (Binary) Streams
- At the lowest level, a stream is a sequence of binary numbers
- The simplest IO classes break up the sequence into 8 - bit chunks, called bytes. Each byte corresponds to an integer in the range 0 – 255. 11000101001011101011011010101010100101….. 197 46 182 170
Binary IO example
InputStream fin = new FileInputStream( filename); int[][] data = new int[ width][ height]; for (int i=0; i < data.length; i++) { for (int j=0; j < data[0].length; j++) { int ch = fin.read(); if (ch == - 1) { fin.close(); throw new IOException("File ended early"); } data[j][i] = ch; } } fin.close();
BufferedInputStream
- Reading one byte at a time can be slow!
- Each time a stream is read there is a fixed overhead, plus time proportional to the number of bytes read. disk - > operating system - > JVM - > program disk - > operating system - > JVM - > program disk - > operating system - > JVM - > program
- A BufferedInputStream presents the same interface to clients, but internally reads many bytes at once into a buffer (incurring the fixed overhead only once) disk - > operating system - >>>> JVM - > program JVM - > program JVM - > program JVM - > program
The Standard Java Streams
java.lang.System provides an InputStream and two standard PrintStream objects for doing console I/O.
System.in
System.err
System.out
Note that System.in, for example, is a static member of the class System – this means that the field “in” is associated with the class , not an instance of the class. Recall that static members in Java act like global variables.
PrintStream Methods
- Note the use of overloading : there are multiple methods called println
- The compiler figures out which one you mean based on the number of arguments, and/or the static type of the argument you pass in at the method’s call site.
- The java I/O library uses overloading of constructors pervasively to make it easy to “glue together” the right stream processing routines void println(boolean b); // write b followed by a new line void println(String s); // write s followed by a newline void println(); // write a newline to the stream void print(String s); // write s without terminating the line (output may not appear until the stream is flushed) void flush(); // actually output characters waiting to be sent
PrintStream adds buffering and binary-conversion
methods to OutputStream
Reader and Writer
- Similar to the InputStream and OutputStream classes, including:
- These operations read and write int values that represent unicode characters - read returns an integer in the range 0 to 65535 (i.e. 16 bits) - value - 1 represents “no more data” (when returned from read) - requires an “encoding” (e.g. UTF-8 or UTF-16, set by a Locale)
- Like byte streams, java.io provides many subclasses of Reader and Writer
- e.g. FileReader / FileWriter
- use classes these for portable text I/O
- Gotcha: System.in, System.out, System.err are byte streams
- So wrap in an InputStreamReader / PrintWriter if you need unicode console I/O int read (); // Reads the next character void write (int b); // Writes the char to the output
Design Example: Histogram.java
A design exercise using java.io and the generic collection libraries (SEE COURSE NOTES)
Decompose the problem
- Sub-problems:
- How do we iterate through the text file, identifying all of the words?
- Once we can produce a stream of words, how do we calculate their frequency?
- Once we have calculated the frequencies, how do we print out the result?
- What is the interface between these components?
- Can we test them individually?
Coding: Histogram.java
WordScanner.java Histogram.java