0% found this document useful (0 votes)
44 views

Input Output

The document discusses input/output (I/O) in Java. It covers I/O basics like byte and character streams, predefined System streams, reading console input using BufferedReader, reading/writing characters and strings, writing console output using PrintStream, and using the PrintWriter class. The key classes covered are InputStream, OutputStream, Reader, Writer, BufferedReader, InputStreamReader, PrintStream, and PrintWriter.

Uploaded by

Rishi Phal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Input Output

The document discusses input/output (I/O) in Java. It covers I/O basics like byte and character streams, predefined System streams, reading console input using BufferedReader, reading/writing characters and strings, writing console output using PrintStream, and using the PrintWriter class. The key classes covered are InputStream, OutputStream, Reader, Writer, BufferedReader, InputStreamReader, PrintStream, and PrintWriter.

Uploaded by

Rishi Phal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

Input/Output

G A N E S H PA I
A S S T. P R O F E S S O R G D I I I
D E PA RT M E N T O F C S E
N M A M I T, N I T T E
Overview of topics
 I/O Basics
 Reading Console Input
 Writing Console Output
 Print Writer Class

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 2


 Java programs perform I/O through streams
 A stream is an abstraction that either produces or consumes
information.
 A stream is linked to a physical device by the Java I/O system.
 All streams behave in the same manner, even if the actual
Input/Output physical devices to which they are linked differ.
I/O Basics  The same I/O classes and methods can be applied to different
Reading Console Input types of devices.
Writing Console Output  An input stream can abstract different kinds of input: disk file,
Print Writer Class a keyboard, or a network socket.
 An output stream may refer to the console, a disk file, or a
network connection
 Java implements streams within class hierarchies defined in
the java.io package.

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 3


I/O Basics
 Java defines two types of streams
 Byte streams
 Character streams
 Byte streams
 Provide a convenient means for handling input and output of bytes.
 Are used, for example, when reading or writing binary data.
 Character streams
 Provide a convenient means for handling input and output of characters.
 They use Unicode and, therefore, can be internationalized.

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 4


Byte Stream Classes
 Byte streams are defined by using two class
hierarchies.
 Top two abstract classes:
 InputStream
 OutputStream

 These abstract classes has several concrete


subclasses that handle the differences among various
devices, such as disk files, network connections, and
memory buffers.
 InputStream and OutputStream define several key
methods that other stream classes implement.
 Important ones: read( ), write( )

 Each has a form that is abstract and must be


overridden by derived stream classes

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 5


Byte Stream Classes

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 6


Character Stream Classes
 Character streams are defined by using two
class hierarchies.
 Top two abstract classes:
 Reader
 Writer

 These abstract classes handle Unicode


character streams and has several concrete
subclasses for each of these
 Abstract classes Reader and Writer define
several key methods that other stream classes
implement.
 Important methods: read( ), write( ), which
read and write characters of data
 Each has a abstract form that must be
overridden by derived stream classes

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 7


Character Stream Classes

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 8


Predefined Streams
 All Java programs automatically import the java.lang package.
 java.lang defines a class called System, which encapsulates several aspects of the run-time
environment
 System contains three predefined stream variables
 in, out, and err.
 These fields are declared as public, static, and final within System.
 System.in refers to standard input, which is the keyboard by default.
 System.out refers to the standard output stream, console by default.
 System.err refers to the standard error stream, console by default.
 These streams may be redirected to any compatible I/O device.
 System.in is an object of type InputStream;
 System.out and System.err are objects of type PrintStream.

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 9


Reading Console Input
 Console input is read from System.in.
 Character based stream attached to the console, wrap System.in in a BufferedReader object.
 BufferedReader supports a buffered input stream.
 Commonly used constructor: BufferedReader(Reader inputReader)
inputReader is the stream that is linked to the instance of BufferedReader that is being created.

 One of the concrete subclass of abstract class Reader is InputStreamReader, which converts bytes to
characters.
 InputStreamReader object linked to System.in is obtained using the following constructor:
InputStreamReader(InputStream inputStream)
 Combining all, the following line of code creates a BufferedReader that is connected to the keyboard:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 br is a character-based stream that is linked to the console through System.in.
Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 10
Reading Characters
 To read a character from a BufferedReader, use read( ).
int read( ) throws IOException
 Each time read( ) is called,
 it reads a character from the input stream
 returns it as an integer value
 returns –1 when the end of stream is encountered

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 11


Reading Characters
import java.io.*;
class BRRead {
public static void main(String args[]) throws IOException
{
char c;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));


System.out.println("Enter characters, 'q' to quit.");

// read characters
do {
c = (char) br.read();
System.out.println(c);
} while(c != 'q');
}
}

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 12


Reading Strings
 readLine( ), a member of the BufferedReader class is used to read a string from the keyboard.
 General form:
String readLine( ) throws IOException
 Returns a String object.

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 13


Reading Strings
// Read a string from console using a BufferedReader.
import java.io.*;
class BRReadLines {
public static void main(String args[]) throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;

System.out.println("Enter lines of text.");


System.out.println("Enter 'stop' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
}
}

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 14


Writing Console Output
 Console output is accomplished with print( ) and println( ) defined by the PrintStream
 PrintStream is type of object referenced by System.out
 Since PrintStream is an output stream derived from OutputStream, it also implements the
low-level method write( ) to write to the console.
void write(int byteval)
writes the byte specified by byteval
// Demonstrate System.out.write().
class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 15


Reading & Writing using Console object
 Java SE 6 adds the Console class.
 Console supplies no constructors.
 A Console object is obtained by calling System.console( )
static Console console( )

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 16


Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 17
Reading & Writing using Console object

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 18


PrintWriter Class
 PrintWriter is a character-based classes.

 One of the constructor


PrintWriter(OutputStream outputStream, boolean flushingOn)
 outputStream is an object of type OutputStream
 flushingOn controls whether Java flushes the output stream every time a println( ) method is called. If
flushingOn is true, flushing automatically takes place. If false, flushing is not automatic.

 PrintWriter supports the print( ) and println( ) methods.

 If an argument is not a simple type, the PrintWriter methods call the object’s toString( ) method and then
display the result.

 To write to the console by using a PrintWriter, specify System.out for the output stream and automatic flushing.

 Ex: Creates a PrintWriter that is connected to console output:

PrintWriter pw = new PrintWriter(System.out, true);

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 19


PrintWriter Class
// Demonstrate PrintWriter
import java.io.*;
public class PrintWriterDemo {
public static void main(String args[]) {
PrintWriter pw = new PrintWriter(System.out, true);

pw.println("This is a string");

int i = -7;
pw.println(i);

double d = 4.5e-7;
pw.println(d);
}
}

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 20


End of Input Output

Input/Output GANESH PAI, DEPT. OF CSE, NMAMIT, NITTE 21

You might also like