CodesJava

Easy learning with example program codes

Input output (I/O) in java


In java Input output operations are based on the concept of stream. The java.io package contains all the required classes to perform I/O operations.

A stream is defined as a sequence of data.

Automatically created streams in java:

  1. System.in: standard input stream.
  2. System.out: standard output stream.
  3. System.err: standard error.

Input stream:

InputStream is used to read data from a source like file, peripheral device etc.

InputStream class:

InputStream class is the super class of all classes representing an input stream. It is an abstract class.

Commonly used methods of InputStream class:

1. read(): Reads the next byte of data from the input stream. It returns -1 at the end of file.

Syntax: public abstract int read()throws IOException.

2. available(): Returns an estimate of the number of bytes that can be read from the current input stream.

Syntax: public int available()throws IOException.

3. close(): It is used to close the current input stream.

Syntax: public void close()throws IOException.

Output Stream:

OutputStream is used for writing data to a destination like file, peripheral device etc.

OutputStream class:

OutputStream class is the super class of all classes representing an output stream. It is an abstract class.

Commonly used methods of OutputStream class:

1. write(int): This method is used to write a byte to the current output stream.

Syntax: public void write(int)throws IOException.

2. write(byte[]): This method is used to write an array of byte to the current output stream.

Syntax: public void write(byte[])throws IOException.

3. flush():flushes the current output stream.

Syntax: public void flush()throws IOException.

4. close(): This method is used to close the current output stream.

Syntax: public void close()throws IOException.
 
Next Topic: FileInputStream and FileOutputStream in java with example.
Previous Topic: Synchronized block in java with example.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.

 

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap