Untitled
Untitled
Untitled
• Streams represent the flow of data and that data can be in any format
• Like (byte, text, primitive data type etc)
• To write data into a destination, the output stream is used
• To read the data, the input stream is used
DIFFERENT FORMATS OF READING AND WRITING:
Types of data
1. Binary data
2. Text data
3. Formatted data
4. Buffering .
INPUT / OUTPUT STREAMS
1. Binary data:
▪ In this way, for every byte, the compiler will send a request to the OS
INPUT / OUTPUT STREAMS
⮚ more test.txt
This is a text file.
It contains two lines.
⮚ hexdump test.txt
0000000 54 68 69 73 20 69 73 20 61 20 74 65 78 74 20 66
0000010 69 6c 65 2e 0a 49 74 20 63 6f 6e 74 61 69 6e 73
0000020 20 74 77 6f 20 6c 69 6e 65 73 2e 0a
000002c
>
INPUT / OUTPUT STREAMS
2. Text data:
▪ the content as primitive data type such us boolean, char, byte, short, int, long, float, double
and String.
INPUT / OUTPUT STREAMS
3. Formatted data:
▪ to translate raw text or binary data to a desired form
● This may involve specialized hardware, for example to play an audio stream or to
display an image.
● A highly versatile and configurable class for parsing input text in a known format
is java.util.Scanner.
● On the output side, java.io.PrintStream provides extensive text formatting
capabilities.
INPUT / OUTPUT STREAMS
4. Buffering:
▪ Access to the underlying input stream may be inefficient if the source is remote
or the medium is slow.
▪ To improve performance, input streams and readers are often wrapped in a
BufferedInputStream or BufferedReader to provide buffering.
InputStream in = new BufferedInputStream(new FileInputStream(fileName));
▪ user runs your program from the console, they can supply arguments on the
command line
▪ These arguments are then available to your program in the String[] array
parameter of the main method.
INPUT SOURCES
Example
⮚ if you type in a console window
java MyProgram a b "c d" e
a
b
c d
e
INPUT SOURCES
In Eclipse, you can supply the command line arguments under the Arguments tab in
the run configuration.
CONSOLE (USER INPUT)
● input typed in at the keyboard by the user while the program is running
● There is a built-in InputStream just for this purpose, called System.in
● Often this is wrapped in an instance of Scanner to read the input one line at a
time
Scanner sysin = new Scanner(System.in);
System.out.print("Please type something: ");
String s = sysin.nextLine();
System.out.println("You typed: " + s);
● You can create an InputStream from a binary file or a Reader from a text file by
supplying the path name to the constructor.
● A resource is a file with some data that your program uses internally
Example: background image, audio clip, or text
● Java looks for resources using your program’s Class Loader
● resources must be on the class path so that the ClassLoader can find them.
Console: