Streams

Streams:
Java program performs 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 different types of devices. this means that input streams can abstract many different kinds of input: from a disk file, a keyboard, or a network socket.

Stream class
Stream class

Byte Streams and Character Streams:
java defines two types of streams: byte and character. Byte streams provide a convenient means for handling input and output of bytes. Bytes streams are used, for example, when reading or writing binary data. Character streams provide a convenient means for handling input and output of characters. They use Unicode and, therefore, can be internationalized Also, in some cases, character streams are more efficient than byte streams. One another point at the lowest level, all I/O is still byte-oriented. the character-based streams simply provide a convenient and efficient means for handling characters.

The Character Streams classes:
Character stream is defined by using two-class hierarchies. At the top are two abstract classes: Reader and Writer. These abstract classes handle Unicode character streams.Java has several concrete subclasses of each of these. the character streams classes in java.io are:

 Character Streams classes
Character Streams classes

The Predefined Streams:
The system also contains three predefined stream variables: in, out and err.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 an object of type Input Stream; System.out and System.err are objects of type Print Stream. These are byte streams, even though they are typically used to read and write characters from and to the console.

*Reading characters
To read character from a BufferedReader, use read(). the version of read() that we will be using is
int read() throwsIOException
read() is called, it reads a character from the input stream and returns it as an integer value.
1.example for reading characters

import java.util.*;
class BR
{
public static void main(String args[]) throws IOException
{
char c;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the characters,'q' to quit.");
//read characters
do
{
c = (char) br.read();
System.out.println(c);
}while(c!='q');
}
}
OUTPUT:
Enter the characters, 'q' to quit.
123abcq
1
2
3
a
b
c
q

2.CREATE A folder

import java.util.*;
import java.io.*;
class Folder
{
public static void main(String args[])
{
String s="C:/Abc/Xyz";
File f=newFile(s);
try
{
if(f.exists()==false)
{
f.mkdirs();
System.out.println("Directory Created");
}
else
{
System.out.println("Directory is already present.");
String sa="C:/Abc/Xyz/NewFolder";
File fa = new File(sa);
fa.mkdir();
System.out.println("New Folder is created.");
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}

OUTPUT:
(1st Run)
//If c:/Abc/Xyz does not exists

Directory Created
//Abc and Xyz folders are created
(2nd Run)
//If c:/Abc/Xyz exists
Directory is alredy present new folder is created 
//new folder is created inside Xyz folder.

Translate »