Java I/O
Chittaranjan Pradhan
Web Technology 13 I/O Basics
Stream
Java I/O Reading Console Input
Reading with
java.util.Scanner class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
13.1
Java I/O
I/O Basics
Chittaranjan Pradhan
I/O Basics
Stream
Reading Console Input
I/O Basics Reading with
java.util.Scanner class
Writing Console
• Java I/O is used to process the input and produce the Output
output based on the input PrintWriter Class
Reading & Writing
• Java uses the concept of stream to make I/O operations Files
Character-based Reading &
• java.io package contains all the classes required for input Writing
and output operations
• Two important methods are read() and write()
13.2
Java I/O
Stream
Chittaranjan Pradhan
Stream
I/O Basics
• A stream is an abstraction that either produces or Stream
Reading Console Input
consumes information. It is linked to a physical device by
Reading with
the Java IO system java.util.Scanner class
Writing Console
• A stream is a sequence of data. It is composed of bytes Output
• java.lang package defines a class called System, which PrintWriter Class
Reading & Writing
encapsulates several aspects of the run-time environment Files
Character-based Reading &
• System.out: Writing
• It refers to the standard output stream
• System.in:
• It refers to the standard input stream
• System.err:
• It refers to the standard error stream
System.in is an object of type InputStream. System.out and
System.err are objects of type PrintStream
13.3
Java I/O
Stream...
Chittaranjan Pradhan
I/O Basics
Byte Stream Classes Stream
Reading Console Input
• These are defined by using two class hierarchies at the Reading with
top: InputStream and OutputStream java.util.Scanner class
Writing Console
• To use stream classes, you must import java.io Output
PrintWriter Class
Reading & Writing
• OutputStream: Files
Character-based Reading &
• It is an abstract class. It is the superclass of all classes Writing
representing an output stream of bytes
• It is used to write data to a destination
• InputStream:
• It is an abstract class. It is the superclass of all classes
representing an input stream of bytes
• It is used to read data from a source
13.4
Java I/O
Stream...
Chittaranjan Pradhan
Character Stream Classes I/O Basics
Stream
• These are defined by using two class hierarchies at the Reading Console Input
top: Reader and Writer Reading with
java.util.Scanner class
• These classes handle Unicode character streams Writing Console
Output
PrintWriter Class
• Reader: Reading & Writing
Files
• It is the abstract class that describes character stream input Character-based Reading &
Writing
• Reader class contains methods that are identical to those
available in InputStream class, except Reader is designed
to handle characters
• Writer:
• It is the abstract class that describes character stream
output
• It provides support for all output operations by defining
methods that are identical to those in OutputStream class
13.5
Java I/O
Reading Console Input
Chittaranjan Pradhan
Reading Console Input
I/O Basics
• Console input is accomplished by reading from System.in. Stream
To obtain a character based stream that is attached to Reading Console Input
Reading with
console, wrap System.in in a BufferedReader object java.util.Scanner class
• BufferedReader br=new BufferedReader(new Writing Console
Output
InputStreamReader(System.in)); PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Reading Characters Writing
• int read() throws IOException
• char ch=(char)br.read();
Reading Strings
• String readLine() throws IOException
• String str=br.readLine();
13.6
Java I/O
Reading Console Input...
Chittaranjan Pradhan
Reading Integers
I/O Basics
• int Integer.parseInt(br.readLine());
Stream
• int item=Integer.parseInt(br.readLine()); Reading Console Input
Reading with
java.util.Scanner class
Reading Other Types of Values Writing Console
Output
PrintWriter Class
• Float.parseFloat(br.readLine()) Reading & Writing
Files
Character-based Reading &
Writing
• Double.parseDouble(br.readLine())
• Byte.parseByte(br.readLine())
• Short.parseShort(br.readLine())
• Long.parseLong(br.readLine())
• Boolean.parseBoolean(br.readLine())
13.7
Java I/O
Reading with java.util.Scanner class
Chittaranjan Pradhan
I/O Basics
Stream
Reading with java.util.Scanner class Reading Console Input
Reading with
java.util.Scanner class
• Scanner sc=new Scanner(System.in);
Writing Console
• When the Scanner class receives input, it breaks it into Output
PrintWriter Class
several pieces, called tokens
Reading & Writing
• String str=sc.next(); Files
Character-based Reading &
String str=sc.nextLine(); Writing
char ch=sc.next.charAt(0);
int item=sc.nextInt();
float bal=sc.nextFloat();
long a=sc.nextLong();
long b=sc.nextDouble();
13.8
Java I/O
Writing Console Output
Chittaranjan Pradhan
I/O Basics
Stream
Reading Console Input
Reading with
Writing Console Output java.util.Scanner class
Writing Console
Output
• Console output is accomplished by print() and println().
PrintWriter Class
These methods are defined by PrintStream class Reading & Writing
Files
• PrintStream is an output stream derived from Character-based Reading &
Writing
OutputStream. It also implements write()
• int b=’P’;
System.out.write(b);
13.9
Java I/O
PrintWriter Class
Chittaranjan Pradhan
I/O Basics
Stream
PrintWriter Class Reading Console Input
Reading with
• PrintWriter is one of the character based classes java.util.Scanner class
Writing Console
• PrintWriter(OutputStream outputStream, boolean Output
flushOnNewline) PrintWriter Class
Reading & Writing
• PrintWriter supports print() and println() methods for all Files
types including Object Character-based Reading &
Writing
• To write to the console by using PrintWriter, specify
System.out for the output stream and flush the stream
after each newline
• PrintWriter pw=new PrintWriter(System.out, true);
pw.println("Bye");
13.10
Java I/O
Reading & Writing Files
Chittaranjan Pradhan
I/O Basics
Reading & Writing Files Stream
Reading Console Input
• Java files are byte-oriented Reading with
java.util.Scanner class
• Java allows wrapping of a byte-oriented file stream within Writing Console
a character-based object Output
PrintWriter Class
• Most used stream classes are: FileInputStream and
Reading & Writing
FileOutputStream Files
Character-based Reading &
Writing
• FileInputStream(String fname) throws
FileNotFoundException
FileOutputStream(String fname) throws
FileNotFoundException
• When an output file is opened, any preexisting file by the
same name is destroyed
13.11
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
I/O Basics
Reading & Writing Files... Stream
Reading Console Input
• After the work is over, the file should be closed by using Reading with
close() method java.util.Scanner class
Writing Console
void close() throws IOException Output
PrintWriter Class
• To read from a file, use Reading & Writing
Files
int read() throws IOException Character-based Reading &
Writing
Each time the read() is called, it reads a single byte from
the file and returns the byte as an integer value
• To write to a file, use
void write(int byteval) throws IOException
Though byteval is declared as an integer, only the
low-order 8 bits are written to the file
13.12
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
I/O Basics
Writing Files
Stream
DataInputStream class is used to read data from keyboard Reading Console Input
DataInputStream dis = new DataInputStream(System.in); Reading with
java.util.Scanner class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.13
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
Reading Files
I/O Basics
Stream
Reading Console Input
Reading with
java.util.Scanner class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.14
Java I/O
Reading & Writing Files...
Chittaranjan Pradhan
Copying Files
I/O Basics
Stream
Reading Console Input
Reading with
java.util.Scanner class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.15
Java I/O
Character-based Reading & Writing
Chittaranjan Pradhan
I/O Basics
Writing Files using FileWriter Stream
FileWriter is useful to create file by writing characters into it Reading Console Input
Reading with
java.util.Scanner class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.16
Java I/O
Character-based Reading & Writing...
Chittaranjan Pradhan
Reading Files using FileReader
FileReader is useful to read data in the form of characters I/O Basics
Stream
Reading Console Input
Reading with
java.util.Scanner class
Writing Console
Output
PrintWriter Class
Reading & Writing
Files
Character-based Reading &
Writing
13.17