You are on page 1of 22

// Example of Console class

import java.io.Console;
class ConsoleClass
{
public static void main(String[] args)
{
Console c = System.console();

String name;
char[] passord;
i!(c "= null){
System.out.println(#$nter uername#);
name = c.read%ine();
passord = c.read&assord(#$nter &assord' #);
System.out.println(#(y username #)name)# *
passord '#)passord);
+
+
+
//Example for the File
import java.io.,ile;
public class -ead.ir {
public static void main(String[] args) {

,ile !ile = null;
String[] paths;
try{
// create ne !ile object
!ile = ne ,ile(#input.t0t#);
// array o! !iles and directory
paths = !ile.list();
// !or each name in the path array
!or(String path'paths)
{
// prints !ilename and directory name
System.out.println(path);
+
+catch($0ception e){
// i! any error occurs
e.printStac12race();
+
+
+
//Example for the FileInput Stream (Byte Stream)
import java.io.34$0ception;
import java.io.,ile3nputStream;
public class ,ile3nputStream.emo {
public static void main(String[] args) thros 34$0ception {
,ile3nputStream !is = null;
int i = 5;
char c;
byte[] bs = ne byte[6];
try{
// create ne !ile input stream
!is = ne ,ile3nputStream(#output.t0t#);
// read bytes to the bu!!er
i=!is.read(bs);
// prints
System.out.println(#7umber o! bytes read' #)i);
System.out.print(#8ytes read' #);
// !or each byte in bu!!er
!or(byte b'bs)
{
// converts byte to character
c=(char)b;
// print
System.out.println(c);
+
+catch($0ception e0){
// i! any error occurs
e0.printStac12race();
+!inally{
// releases all system resources !rom the streams
i!(!is"=null)
!is.close();
+
+
+
//Example for the FilterInput Stream (Byte Stream)
import java.io.8u!!ered3nputStream;
import java.io.,ile3nputStream;
import java.io.,ilter3nputStream;
import java.io.34$0ception;
import java.io.3nputStream;
public class ,ilter3nputStream.emo {
public static void main(String[] args) thros $0ception {
3nputStream is = null;
,ilter3nputStream !is = null;
int i=5;
char c;
try{
// create input streams
is = ne ,ile3nputStream(#input.t0t#);
!is = ne 8u!!ered3nputStream(is);
// read till the end o! the stream
hile((i=!is.read())"=9:)
{
// converts integer to character
c=(char)i;
System.out.println(#Character read' #)c);
+
+catch(34$0ception e){
// i! any 3/4 error occurs
e.printStac12race();
+!inally{

// releases any system resources associated ith the stream
i!(is"=null)
is.close();
i!(!is"=null)
!is.close();
+
+
+
//Example for the BufferedInput Stream (Byte Stream)
import java.io.8u!!ered3nputStream;
import java.io.,ile3nputStream;
import java.io.3nputStream;
public class 8u!!ered3nputStream.emo {
public static void main(String[] args) thros $0ception {
,ile3nputStream inStream = null;
8u!!ered3nputStream bis = null;

try{
// open input stream test.t0t !or reading purpose.
inStream = ne ,ile3nputStream(#input.t0t#);
// input stream is converted to bu!!ered input stream
bis = ne 8u!!ered3nputStream(inStream);
// read until a single byte is available
hile(bis.available();5)
{
// read the byte and convert the integer to character
char c = (char)bis.read();
// print the characters
System.out.println(#Char' #)c);
+
+catch($0ception e){
// i! any 3/4 error occurs
e.printStac12race();
+!inally{
// releases any system resources associated ith the stream
i!(inStream"=null)
inStream.close();
i!(bis"=null)
bis.close();
+
+
+
//Example for the DataInput Stream (Byte Stream)
import java.io..ata3nputStream;
import java.io..ata4utputStream;
import java.io.,ile3nputStream;
import java.io.,ile4utputStream;
import java.io.34$0ception;
import java.io.3nputStream;
public class .ata3nputStream.emo {
public static void main(String[] args) thros 34$0ception {
,ile3nputStream is = null;
.ata3nputStream dis = null;
,ile4utputStream !os = null;
.ata4utputStream dos = null;
double[] dbu! = {<=.=<><<.?@><A.@?><?.?B><@.==>A5.CA+;

try{
// create !ile input stream
is = ne ,ile3nputStream(#input.t0t#);
// create ne data input stream
dis = ne .ata3nputStream(is);
// read till end o! the stream
hile(dis.available();5)
{
// read character
double c = dis.read.ouble();
// print
System.out.print(c ) # #);
+
+catch($0ception e){
// i! any 3/4 error occurs
e.printStac12race();
+!inally{

// releases all system resources !rom the streams
i!(is"=null)
is.close();
// i!(dos"=null)
// dos.close();
i!(dis"=null)
dis.close();
// i!(!os"=null)
// !os.close();
+
+
+
//Example for the ByteArrayInput Stream (Byte Stream)
import java.io.8yteDrray3nputStream;
import java.io.34$0ception;
public class 8yteDrray3nputStream.emo {
public static void main(String[] args) thros 34$0ception {
byte[] bu! = {<=> <<> <A> <?> <@+;
8yteDrray3nputStream bais = null;
try{
// create ne byte array input stream
bais = ne 8yteDrray3nputStream(bu!);
int b =5;
// read till the end o! the stream
hile((b = bais.read())"=9:)
{ // convert byte to character
char c = (char)b;
System.out.println(#byte '#)b)#; char ' #) c);
+
System.out.print(bais.read())# -eached the end#);
+catch($0ception e){
// i! 3/4 error occurs
e.printStac12race();
+!inally{
i!(bais"=null)
bais.close();
+
+
+
//Example for FileOutputStream
import java.io.34$0ception;
import java.io.,ile4utputStream;
public class ,ile4utStream.emo {
public static void main(String[] args) thros 34$0ception {
,ile4utputStream !out = null;
int i = 5;
char c;
byte[] bs = {:B>BC>C=+;
try{
// create ne !ile input stream
!out = ne ,ile4utputStream(#output.t0t#);
// read bytes to the bu!!er
!out.rite(bs);
+catch($0ception e0){
// i! any error occurs
e0.printStac12race();
+!inally{
// releases all system resources !rom the streams
i!(!out"=null)
!out.close();
+
+
+
//Example for FilterOututStream
import java.io.,ile3nputStream;
import java.io.,ile4utputStream;
import java.io.,ilter4utputStream;
import java.io.34$0ception;
import java.io.4utputStream;
public class ,ilter4utputStream.emo {
public static void main(String[] args) thros $0ception {

,ile4utputStream os = null;
,ilter4utputStream !os = null;
,ile3nputStream !is = null;
byte[] in!o = {<=> <<> <A> <?> <@+;
int i=5;
char c;
try{
// create output streams
os = ne ,ile4utputStream(#input.t0t#);
!os = ne ,ilter4utputStream(os);
// rites in!o to the output stream
!os.rite(in!o);
// !orces byte contents to ritten out to the stream
!os.!lush();
+catch(34$0ception e){

// i! any 3/4 error occurs
System.out.print(#Close() is invo1ed prior to rite()#);
+!inally{

// releases any system resources associated ith the stream
i!(os"=null)
os.close();
i!(!os"=null)
!os.close();
+
+
+
//Example for BufferedOututStream
import java.io.8u!!ered4utputStream;
import java.io.8yteDrray4utputStream;
import java.io.34$0ception;
public class 8u!!ered4utputStream.emo {
public static void main(String[] args) thros $0ception {
8yteDrray4utputStream baos = null;
8u!!ered4utputStream bos = null;
try{
// create ne 8yteDrray4utputStream
baos = ne 8yteDrray4utputStream();
// create ne 8u!!ered4utputStream ith baos
bos = ne 8u!!ered4utputStream(baos);
// assign integer
int b = ?A;
// rite to stream
bos.rite(b);
// !orce the byte to be ritten to baos
bos.!lush();
// convert 8yteDrray4utputStream to bytes
byte[] bytes = baos.to8yteDrray();
// prints the byte
System.out.println(bytes[5]);

+catch(34$0ception e){
// i! 3/4 error occurs.
e.printStac12race();
+!inally{
// releases any system resources associated ith the stream
i!(baos"=null)
baos.close();
i!(bos"=null)
bos.close();
+
+
+
//Example for DataOututStream
import java.io..ata3nputStream;
import java.io..ata4utputStream;
import java.io.,ile3nputStream;
import java.io.,ile4utputStream;
import java.io.34$0ception;
import java.io.3nputStream;
public class .ata4utputStream.emo {
public static void main(String[] args) thros 34$0ception {
3nputStream is = null;
.ata3nputStream dis = null;
,ile4utputStream !os = null;
.ata4utputStream dos = null;
double[] dbu! = {<=.=<><<.?@><A.@?><?.?B><@.==>A5.CA+;
try{
// create !ile output stream
!os = ne ,ile4utputStream(#c'EEtest.t0t#);
// create data output stream
dos = ne .ata4utputStream(!os);
// !or each byte in the bu!!er
!or (double d'dbu!)
{
// rite double to the data output stream
dos.rite.ouble(d);
+
// !orce bytes to the underlying stream
dos.!lush();
// create !ile input stream
is = ne ,ile3nputStream(#c'EEtest.t0t#);
// create ne data input stream
dis = ne .ata3nputStream(is);
// read till end o! the stream
hile(dis.available();5)
{
// read character
double c = dis.read.ouble();
// print
System.out.print(c ) # #);
+
+catch($0ception e){ // i! an 3/4 error occurs
e.printStac12race();
+!inally{ // releases all system resources !rom the streams
i!(is"=null)
is.close();
i!(dos"=null)
is.close();
i!(dis"=null)
dis.close();
i!(!os"=null)
!os.close();
+
+
+
//Example for rintStream
import java.io.F;
public class &rintStream.emo {
public static void main(String[] args) {
int 0 = =;
// create printstream object
&rintStream ps = ne &rintStream(System.out);
// print integer
ps.print(0);
ps.println();
ps.print(:55);
// !lush the stream
// ps.!lush();
+
+
//Example for ByteArrayOututStream
import java.io.8yteDrray4utputStream;
import java.io.34$0ception;
public class 8yteDrray4utputStream.emo {
public static void main(String[] args) thros 34$0ception {
String str = ##;
8yteDrray4utputStream baos = null;
try{
// create ne 8yteDrray4utputStream
baos = ne 8yteDrray4utputStream();
// rite byte array to the output stream
baos.rite(<=);
// converts the byte to the de!ault charset value
str = baos.toString();
// prints the string
System.out.println(str);
+catch($0ception e){
// i! 3/4 error occurs
e.printStac12race();
+!inally{
i!(baos"=null)
baos.close();
+
+
+
//Example for File!eader (Character Stream)
import java.io.F;
class !ilereader
{
public static void main(String args[]) thros $0ception
{
,ile-eader !ilereader = ne ,ile-eader(#input.t0t#);
char data[] = ne char[:5B6];
/F
int d;
hile((d= !ilereader.read())"=9:){
System.out.println((char) d);
+
F/
int charsread = !ilereader.read(data);
System.out.println(#Characters read #)charsread);
System.out.println(ne String(data> 5 > charsread));
!ilereader.close();
+
+
//Example for InputStream!eader (Character Stream)
import java.io.,ile3nputStream;
import java.io.34$0ception;
import java.io.3nputStream-eader;
public class 3nputStream-eader.emo {
public static void main(String[] args) thros 34$0ception {
,ile3nputStream !is = null;
3nputStream-eader isr =null;
char c;
int i;
try {
// ne input stream reader is created
!is = ne ,ile3nputStream(#input.t0t#);
isr = ne 3nputStream-eader(!is);

// read till the end o! the !ile
hile((i=isr.read())"=9:)
{
// int to character
c=(char)i;
System.out.println(#Character -ead' #)c);
+
+ catch ($0ception e) {

// print error
e.printStac12race();
+ !inally {

// closes the stream and releases resources associated
i!(!is"=null)
!is.close();
i!(isr"=null)
isr.close();
+
+
+
//Example for Buffered!eader (Character Stream)
import java.io.8u!!ered-eader;
import java.io.,ile3nputStream;
import java.io.3nputStream;
import java.io.3nputStream-eader;
public class 8u!!ered-eader.emo {
public static void main(String[] args) thros $0ception {

,ile3nputStream is = null;
3nputStream-eader isr = null;
8u!!ered-eader br = null;
try{
// open input stream test.t0t !or reading purpose.
is = ne ,ile3nputStream(#input.t0t#);
// create ne input stream reader
isr = ne 3nputStream-eader(is);
// create ne bu!!ered reader
br = ne 8u!!ered-eader(isr);
int value=5;

// reads to the end o! the stream
hile((value = br.read()) "= 9:)
{
// converts int to character
char c = (char)value;
// prints character
System.out.println(c);
+

+catch($0ception e){
e.printStac12race();
+!inally{

// releases resources associated ith the streams
i!(is"=null)
is.close();
i!(isr"=null)
isr.close();
i!(br"=null)
br.close();
+
+
+
//Example for Strin"!eader
import java.io.F;
public class String-eader.emo {
public static void main(String[] args) {
String s = #Gello Horld#;
// create a ne String-eader
String-eader sr = ne String-eader(s);
try {
// read the !irst !ive chars
!or (int i = 5; i I =; i))) {
char c = (char) sr.read();
System.out.print(## ) c);
+
// close the stream
sr.close();
+ catch (34$0ception e0) {
e0.printStac12race();
+
+
+
//Example for OutputStream#riter
import java.io.F;
public class 4utputStreamHriter.emo {
public static void main(String[] args) {
try {
// create a ne 4utputStreamHriter
4utputStream os = ne ,ile4utputStream(#test.t0t#);
4utputStreamHriter riter = ne 4utputStreamHriter(os);
// create a ne ,ile3nputStream to read hat e rite
,ile3nputStream in = ne ,ile3nputStream(#test.t0t#);
// rite something in the !ile
riter.rite(A5);
riter.rite(A:);
riter.rite(AB);
// !lush the stream
riter.!lush();
// read hat e rite
!or (int i = 5; i I C; i))) {
System.out.print(## ) (char) in.read());
+
+ catch ($0ception e0) {
e0.printStac12race();
+
+
+
//Example for File#riter
import java.io.,ilterHriter;
import java.io.StringHriter;
import java.io.Hriter;
public class ,ilterHriter.emo {
public static void main(String[] args) thros $0ception {

,ilterHriter ! = null;
Hriter = null;
String s=null;
try{
// create ne reader
= ne StringHriter(<);
// !ilter riter
! = ne ,ilterHriter() {
+;
// rite to !ilter riter
!.rite(<=);
!.rite(<<);
!.rite(<A);
// get the string
s = .toString();
// print
System.out.print(#String' #)s);
+catch($0ception e){

// i! any 3/4 error occurs
e.printStac12race();
+!inally{

// releases system resources associated ith this stream
i!("=null)
.close();
i!(!"=null)
!.close();
+
+
+
//Example for Buffered#riter
import java.io.8u!!eredHriter;
import java.io.34$0ception;
import java.io.StringHriter;
public class 8u!!eredHriter.emo {
public static void main(String[] args) thros 34$0ception {
StringHriter s = null;
8u!!eredHriter b = null;
try{
// create string riter
s = ne StringHriter();
//create bu!!ered riter
b = ne 8u!!eredHriter(s);
// integer range represents alphabets in uppercase
!or(int i = <=; iI=@5; i)))
{
b.rite(i);
+
// !orces out the characters to string riter
b.!lush();
// string bu!!er is created
String8u!!er sb = s.get8u!!er();
// prints the string
System.out.println(sb);
+catch(34$0ception e){
// i! 3/4 error occurs
e.printStac12race();
+!inally{
// releases any system resources associated ith the stream
i!(s"=null)
s.close();
i!(b"=null)
b.close();
+
+
+
//Example for Strin"#riter
import java.io.F;
public class StringHriter.emo {
public static void main(String[] args) {
// create a ne riter
StringHriter s = ne StringHriter();
// rite integers that correspond to ascii code
s.rite(A5);
s.rite(A<);
// print result by converting to string
System.out.println(## ) s.toString());
+
+
//Example for O$%ect Seriali&ation usin" O$%ectInputStream and O$%ect Out Stream
//student'%a(a
import java.io.SerialiJable;
class student implements SerialiJable{
public String name;
public int roll7umber;
public String Kuali!ication;
+
//!ecord)"mnt'%a(a
import java.io.F;
class -ecord(gmnt{
student st = null;
,ile4utputStream out;
4bject4utputStream o4ut;
public void initialiJaStudent3n!o(String name> int rn> String Kl){
st = ne student();
st.name = name;
st.roll7umber = rn;
st.Kuali!ication = Kl;
+
public void save.ata()
{
try{
out = ne ,ile4utputStream(#students3n!o.dat#);
o4ut = ne 4bject4utputStream(out);
initialiJaStudent3n!o(#abc#> :55> #(2ech#);
o4ut.rite4bject(st);
System.out.println(st);
initialiJaStudent3n!o(#de!#> :5:> #8$#);
o4ut.rite4bject(st);
System.out.println(st);
System.out.println(#Save in!o success!ully#);
o4ut.close();
out.close();
+catch($0ception tt){+
+
public void load.ata()
{
try{
,ile3nputStream in = ne ,ile3nputStream(#students3n!o.dat#);
4bject3nputStream i3n = ne 4bject3nputStream(in);
4bject obj = null;
hile (true)
{
obj = i3n.read4bject();
i!(obj == null)
brea1;
else{
st = (student) obj;
System.out.println(st.name)#99999#)st.roll7umber);
+
System.out.println(#999999999#);
+
i3n.close();
in.close();
+
catch($4,$0ception ee!){+
catch($0ception tt){ tt.printStac12race();+
+
public static void main(String a[]){
-ecord(gmnt rm = ne -ecord(gmnt();
rm.save.ata();
rm.load.ata();
+
+
//Example for Stream*o+eni&er
import java.io.F;
public class Stream2o1eniJer.emo {
public static void main(String[] args) {
String te0t = #Gello. 2his is a te0t En that ill be split #
) #into to1ens. :):=B#;
try {
// create a ne !ile ith an 4bject4utputStream
,ile4utputStream out = ne ,ile4utputStream(#test.t0t#);
4bject4utputStream oout = ne 4bject4utputStream(out);
// rite something in the !ile
oout.riteL2,(te0t);
oout.!lush();
// create an 4bject3nputStream !or the !ile e created be!ore
4bject3nputStream ois =
ne 4bject3nputStream(ne ,ile3nputStream(#test.t0t#));
// create a ne to1eniJer
-eader r = ne 8u!!ered-eader(ne 3nputStream-eader(ois));
Stream2o1eniJer st = ne Stream2o1eniJer(r);
// print the stream to1ens
boolean eo! = !alse;
do {
int to1en = st.ne0t2o1en();
sitch (to1en) {
case Stream2o1eniJer.22M$4,'
System.out.println(#$nd o! ,ile encountered.#);
eo! = true;
brea1;
case Stream2o1eniJer.22M$4%'
System.out.println(#$nd o! %ine encountered.#);
brea1;
case Stream2o1eniJer.22MH4-.'
System.out.println(#Hord' # ) st.sval);
brea1;
case Stream2o1eniJer.22M7L(8$-'
System.out.println(#7umber' # ) st.nval);
brea1;
de!ault'
System.out.println((char) to1en ) # encountered.#);
i! (to1en == N"N) {
eo! = true;
+
+
+ hile ("eo!);
+ catch ($0ception e0) {
e0.printStac12race();
+
+
+

You might also like