- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Streams
Java programs perform Input Output (I/O) through the streams. A stream is an abstraction that either produces or consumes the information.
A stream is linked to a physical device by the Java I/O system.
All the 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 the methods can be applied to the different types of devices. This means that an input stream can abstract many different kinds of input, from a keyboard, a disk file, or a network socket. Likewise, an output stream may refer to the console, a disk file, or a network connection. Streams are a clean way to deal with input/output without having every part of your code understand the difference between a keyboard and a network, for example. Java implements the streams within class hierarchies defined in the java.io package.
Types of Stream
Java defines the following two types of streams :
- Byte streams
- Character streams
Java Byte Stream
The Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data.
Byte streams are defined by using two hierarchies. At the top are the following two abstract classes :
- InputStream
- OutputStream
Each of these abstract classes has several concrete subclasses that handle the differences among the various devices, such as disk files, network connections, and even memory buffers.
The byte stream classes in the java.io package are shown in the Following table :
Remember - To use the stream classes, you must import the java.io package.
Stream Class | Meaning |
---|---|
BufferedInputStream | Buffered input stream |
BufferedOutputStream | Buffered output stream |
ByteArrayInputStream | Input stream that reads from a byte array |
ByteArrayOutputStream | Output stream that writes to a byte array |
DataInputStream | An input stream that contains the methods for reading the Java standard data types |
DataOutputStream | An output stream that contains the methods for writing the Java standard data types |
FileInputStream | Input stream that reads from a file |
FileOutputStream | Output stream that writes to a file |
FilterInputStream | Implements InputStream |
FilterOutputStream | Implements OutputStream |
InputStream | Abstract class that describes stream input |
ObjectInputStream | Input stream for objects |
ObjectOutputStream | Output stream for objects |
OutputStream | Abstract class that describes stream output |
PipedInputStream | Input pipe |
PipedOutputStream | Output pipe |
PrintStream | Output stream that contains the print() and println() |
PushbackInputStream | Input stream that supports one-byte "unget", which returns a byte to the input stream |
SequenceInputStream | Input stream that is a combination of two or more input streams that will be read sequentially, one after the other |
The abstract classes, InputStream and OutputStream define several key methods that the other stream classes implement. Two of the most important are read() and write() methods, respectively, which read and write bytes of data. Each has a form that is abstract and must be overridden by the derived stream classes.
Java Character Stream
The Character stream provide a convenient means for handling input and output of characters. They are Unicode and, therefore, can be internationalized. Also, in some cases, character streams are more efficient than the byte streams.
Character streams are defined by using the two class hierarchies. At the top are two abstract classes :
- Reader
- Writer
These abstract classes handle the Unicode character streams.
Java has several concrete subclasses of each of these. The character stream classes in the java.io package are shown in the following table :
String Class | Meaning |
---|---|
BufferedReader | Buffered input character stream |
BufferedWriter | Buffered output character stream |
CharArrayReader | Input stream that reads from a character array |
CharArrayWriter | Output stream that writes to a character array |
FileReader | Input stream that reads from a file |
FileWriter | Output stream that writes to a file |
FilterReader | Filtered reader |
FilterWriter | Filtered writer |
InputStreamReader | Input stream that translates bytes to characters |
LineNumberReader | Input stream that counts lines |
OutputStreamWriter | Output stream that translates characters to bytes |
PipedReader | Input pipe |
PipedWriter | Output pipe |
PrintWriter | Output stream that contains the print() and println() |
PushbackReader | Input stream that allows the character to be returned to the input stream |
Reader | Abstract class that describes the character stream input |
StringReader | Input stream that reads from a string |
StringWriter | Output stream that writes to a string |
Writer | Abstract class that describes the character stream output |
The abstract classes, Reader and Writer define several key methods that the other stream classes implement. Two of the most important methods are read() and write(), which read and write the characters of data, respectively. Each has a form that is abstract and must be overridden by the derived stream classes.
Java Predefined Streams
As you already know, all Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of run-time environment. For example, using some of its methods, you can obtain the current time and settings of the various properties associates with the system.
System also contains the following three predefined stream variables:
- in - System.in refers to the standard input, which is the keyboard by default
- out - System.out refers to the standard output stream. By default, this is the console
- err - System.err refers to the standard error stream, which also is the console by default.
However, these streams may be redirected to any compatible I/O devices.
These fields are declared as public, static, and final within System. This means that they can be used by any other part of your program and without reference to a specific System object.
System.in is an object of type InputStream, System.out and System.err are objects of type PrintStream. These are the byte streams, even though they are typically used to read and write the characters from and to the console. As you will see, you can wrap these within the character-based streams, if desired.
Examples on Files in Java
Here are some examples related to files in Java, you can go for.
- Read File in Java
- Write to File Java
- Read & Display File in Java
- Copy File in Java
- Merge two Files in Java
- List files in a Directory Java
- Delete File in Java
« Previous Tutorial Next Tutorial »