Java IO Stream
ref: http://www.studytonight.com/java/java-io-stream.php
IO Stream
Java performs I/O through Streams. A stream is linked to physical layer by java I/O system to make input and
output operation in java. In general, a stream means continuous flow of data. Streams are clean way to deal with input/output without having every part of your code understand the physical
Java encapsulated Stream under java.io package. Java defined two types if stream:
1. Byte Stream: handle input/output of byte
2. Character Stream: handle input/output of characters. Character stream use Unicode and therefore can be internationalized
Byte Stream Classes
Byte Stream is defined by using two abstract class at the top of hierarchy, they are InputStream and OutputStream
## These two abstract classes have several concrete classes that handle various devices
such as disk files, network connection etc.
Some important 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 writting 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() mehod
The above classes define several key methods. Two most important are:
read(): read byte of data
write(): write byte data
***************************************************************************
Character Stream Classes
character stream is also defined by using two abstract class at the top of hierarchy, that are Reader and writer
These two abstract classes have several concrete classes that handle unicode character
stream class description
---------------------------------------------------------------------------------------------------------------------------------------
BufferedReader handles buffered input stream
BufferedWriter handles buffered output stream
InputStreamReader input stream that translate byte to character
OutputStreamWriter output stream that translate character to byte
FileReader input stream reads from file
FileWriter output stream that writes to file
Reader abstract class that define character stream input
Writer abstract class that define character stream output
PrintWriter output stream that contain print() and println() method
Reading Console Input
we use BufferedReader class obj to read inputs from keyboard
`` BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Reading Characters
read() method is used with BufferedReader object to read characters. As this function returns integer type
we need to use typecasting to convert it to char type
`` int read() throws IOException
class CharRead { public static void main( String args[]) { BufferedReader br = new Bufferedreader(new InputstreamReader(System.in)); char c = (char)br.read(); //Reading character } }
Reading Strings
readLine() function with BufferedReader class
`` String readLine() throw IOException
`` String line = br.readLine();
import java.io.*;
class MyInput
{
public static void main(String[] args)
{
String text;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
text = br.readLine(); //Reading String
System.out.println(text);
}
}
Program to read from a file using BufferedReader class
import java. Io *; class ReadTest { public static void main(String[] args) { try { File fl = new File("d:/myfile.txt"); BufferedReader br = new BufferedReader(new FileReader(fl)) ; String str; while ((str=br.readLine())!=null) { System.out.println(str); } br.close(); fl.close(); } catch (IOException e) { e.printStackTrace(); } }
Program to write to a File using FileWriter class
import java. Io *; class WriteTest { public static void main(String[] args) { try { File fl = new File("d:/myfile.txt"); String str="Write this string to my file"; FileWriter fw = new FileWriter(fl) ; fw.write(str); fw.close(); fl.close(); } catch (IOException e) { e.printStackTrace(); } } }