Java IO Stream

Source: Internet
Author: User

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 is clean-deal with input/output without have every part of your code understand the physical

Java encapsulated Stream under Java.io package. Java defined 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 is internationalized

Byte Stream Classes

Byte Stream is defined by using both abstract class at the top of hierarchy, they is InputStream and OutputStream

# # These the abstract classes has 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 this 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. The most important is:

Read (): Read byte of data

Write (): Write byte data

Character Stream Classes

Character stream is also defined by using both abstract class at the top of the hierarchy, which is Reader and WRI ter

These the abstract classes has 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 the BufferedReader class obj to the 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) br.read ();         }}


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 InputStreamReader (system.in);  BufferedReader BufferedReader (ISR);  Text = Br.readline ();             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 ();}}}

Java IO Stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.