Input & Output Classes - Java and OOP
[Pages:57]Input & Output Classes
Java's Hierarchy of Input/Output Classes and Their Purpose
Introduction
Java has a hierarchy of input classes for different purposes.
The base class is InputStream, which reads input as bytes. It has many subclasses, like FileInputStream and DataInputStream.
Other input classes convert the input into characters, strings, or primitive data types. ? Those classes (almost) all get the bytes they need from in InputStream.
Java's output classes use a similar design. .
Sources of Input
An application can read input from: console or terminal file on disk, USB drive, etc. URL on the internet a sensor, using its device driver a microphone or camera, using its device driver another process (output of another application) other sources
What's in a File?
A file contains bytes, grouped into blocks (usually 512 bytes per block). Only bytes -- 0's and 1's. Not text, characters, or images.
Only Bytes.
What about Text Files? PNG?
Text, images, sound, etc. depend on how the application interprets the bytes in a file.
JPEG, PNG, GIF - a standard for how to read and write bytes that represent an image.
MP3 - a standard for how to interpret bytes so they can be rendered (by software) as sound.
TXT - bytes represent codes for characters, using a standard character encoding. Depending on the encoding, one character may be more than one byte in a file.
Text File Example
Suppose we want to write this to a file:
cat
What data is actually written to the file?
If we use ASCII or Unicode UTF-8 encoding for the characters, the file would contain:
binary
hexadecimal
0110001101100001 0111011100001010
6361740A
Hex 63 is the code for 'c'. The last byte (hex 0A) is a newline. It is added by a text editor or Java's System.out.println(). Each hex digit represents 4 bits, e.g. 3=0011, 6=0110, A=1010,
Text File Example Explained
1. "encode" each character according to a character set and encoding. For ASCII, UTF-8, and many others: 'c' = x63 (hexadecimal 63, or binary 0110 0001) 'a' = x61 't' = x74
2. at the end of each "line" of text is a line-end character. This is usually newline (hex 0A): '\n' = x0A Old MacOS apps use a "carriage return" (hex 0D) instead of newline.
Numerical Example
How can we save the value of Math.PI to a file?
3.141592653589793
If we write it as text (characters) the result would be hexadecimal view of file (spaces added for clarity): 34312e33 32393531 35333536 39373938 00000a33
20 bytes. But this is a waste! Math.PI is a double value using 8 bytes in memory.
If we write the actual binary value (as in memory) to file, it would only need 8-bytes, and no data lost in converting to decimal digits.
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- java files copyright © tutorialspoint
- exploring java i o unit 4 exploring java i o
- serial communication in java with example program idc online
- java s input output github pages
- long n method example tutorialspoint
- intermediate programming adelphi university
- java files and i o
- input output classes java and oop
- chapter 20 streams and binary input output
- binary i o university of california san diego
Related searches
- java input output examples
- java input output file
- input output in java
- java input output stream
- input output model example
- math input output tables calculator
- online input output calculator
- input output calculator for functions
- input output solver
- input output table
- input output equation calculator
- input output calculator for math