Java – Input/Output and Files

Download Report

Transcript Java – Input/Output and Files

Java – Input/Output and Files
Streams
• 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 physical
devices to which they are linked differ. Thus, the same I/O classes
and methods can be applied to any type of device.
• This means that an input stream can abstract many different kinds
of input: from a disk file, a keyboard, or a network socket.
• Likewise, 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.
• Stream I/O operations involve three steps:
• Open an input/output stream associated with a physical device
(e.g., file, network, console/keyboard), by constructing an
appropriate I/O stream instance.
• Read from the opened input stream until "end-of-stream"
encountered, or write to the opened output stream (and
optionally flush the buffered output).
• Close the input/output stream.
• http://www.ntu.edu.sg/home/ehchua/programming/java/J5b_I
O.html
• Byte Stream : It provides a convenient means
for handling input and output of byte.
• Character Stream : It provides a convenient
means for handling input and output of
characters. Character stream uses Unicode
and therefore can be internationalized.
Byte and Character Stream classes
Byte Stream classes
Stream class
Description
BufferedInputStream
Used for Buffered Input Stream.
BufferedOutputStream
Used for Buffered Output Stream.
DataInputStream
Contains method for reading java standard
datatype
DataOutputStream
An output stream that contain method for
writing java standard data type
FileInputStream
Input stream that reads from a file
FileOutputStream
Output stream that write to a file.
InputStream
Abstract class that describe stream input.
OutputStream
Abstract class that describe stream output.
PrintStream
Output Stream that
contain print() and println() method
Character Stream Classes
Stream class
Description
BufferedReader
Handles buffered input stream.
BufferedWriter
Handles buffered output stream.
FileReader
Input stream that reads from file.
FileWriter
Output stream that writes to file.
InputStreamReader
Input stream that translate byte to character
OutputStreamReader
Output stream that translate character to byte.
PrintWriter
Output Stream that
contain print() and println() method.
Reader
Abstract class that define character stream input
Writer
Abstract class that define character stream output
Reading Console Input
Reading Characters
• read() method is used with BufferedReader
object to read characters.
• int read() throws IOException
• read( )- it reads a character from the input
stream and returns it as an integer value.
• It returns –1 when the end of the stream is
encountered
// Use a BufferedReader to read characters from the console.
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');
}
}
Enter characters, 'q' to quit.
123abcq
1
2
3
a
b
c
q
Reading Strings
• readLine() function with BufferedReader
class's object.
• String readLine( ) throws IOException
// A tiny editor.
import java.io.*;
class TinyEdit {
public static void main(String args[])
throws IOException
{
// create a BufferedReader using System.in
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str[] = new String[100];
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
for(int i=0; i<100; i++) {
str[i] = br.readLine();
if(str[i].equals("stop")) break;
}
System.out.println("\nHere is your file:");
// display the lines
for(int i=0; i<100; i++) {
if(str[i].equals("stop")) break;
System.out.println(str[i]);
}
}
}
Output
Enter lines of text.
Enter 'stop' to quit.
This is line one.
This is line two.
Java makes working with strings easy.
Just create String objects.
stop
Here is your file:
This is line one.
This is line two.
Java makes working with strings easy.
Just create String objects.
Writing Console Output
• void write(int byteval)
• This method writes to the stream the byte specified by byteval.
• Although byteval is declared as an integer, only the low-order eight bits
are written.
class WriteDemo {
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n');
}
}
The PrintWriter class
• PrintWriter is one of the character-based classes
• PrintWriter defines several constructors.
– PrintWriter(OutputStream outputStream, boolean flushOnNewline)
• outputStream is an object of type OutputStream, and
flushOnNewline controls whether Java flushes the output stream
every time a println( ) method is called.
• If flushOnNewline is true, flushing automatically takes place. If false,
flushing is not automatic.