0% found this document useful (0 votes)
21 views22 pages

Java I/O Package Examples

This document contains examples of using various Java input/output streams including Console, File, FileInputStream, FilterInputStream, BufferedInputStream, ByteArrayInputStream, FileOutputStream, FilterOutputStream, BufferedOutputStream, and DataOutputStream. Each example creates the appropriate stream, performs read or write operations, and closes the stream once complete. Exceptions are caught and printed if errors occur during operations.

Uploaded by

shruthy236
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views22 pages

Java I/O Package Examples

This document contains examples of using various Java input/output streams including Console, File, FileInputStream, FilterInputStream, BufferedInputStream, ByteArrayInputStream, FileOutputStream, FilterOutputStream, BufferedOutputStream, and DataOutputStream. Each example creates the appropriate stream, performs read or write operations, and closes the stream once complete. Exceptions are caught and printed if errors occur during operations.

Uploaded by

shruthy236
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

// Example of Console class

import [Link];
class ConsoleClass
{
public static void main(String[] args)
{
Console c = [Link]();

String name;
char[] passord;
i!(c "= null){
[Link](#$nter uername#);
name = [Link]%ine();
passord = [Link]&assord(#$nter &assord' #);
[Link](#(y username #)name)# *
passord '#)passord);
+
+
+
//Example for the File
import [Link].,ile;
public class -[Link] {
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 = ![Link]();
// !or each name in the path array
!or(String path'paths)
{
// prints !ilename and directory name
[Link](path);
+
+catch($0ception e){
// i! any error occurs
e.printStac12race();
+
+
+
//Example for the FileInput Stream (Byte Stream)
import [Link].34$0ception;
import [Link].,ile3nputStream;
public class ,[Link] {
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=![Link](bs);
// prints
[Link](#7umber o! bytes read' #)i);
[Link](#8ytes read' #);
// !or each byte in bu!!er
!or(byte b'bs)
{
// converts byte to character
c=(char)b;
// print
[Link](c);
+
+catch($0ception e0){
// i! any error occurs
e0.printStac12race();
+!inally{
// releases all system resources !rom the streams
i!(!is"=null)
![Link]();
+
+
+
//Example for the FilterInput Stream (Byte Stream)
import [Link].8u!!ered3nputStream;
import [Link].,ile3nputStream;
import [Link].,ilter3nputStream;
import [Link].34$0ception;
import [Link].3nputStream;
public class ,[Link] {
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=![Link]())"=9:)
{
// converts integer to character
c=(char)i;
[Link](#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)
[Link]();
i!(!is"=null)
![Link]();
+
+
+
//Example for the BufferedInput Stream (Byte Stream)
import [Link].8u!!ered3nputStream;
import [Link].,ile3nputStream;
import [Link].3nputStream;
public class 8u!![Link] {
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([Link]();5)
{
// read the byte and convert the integer to character
char c = (char)[Link]();
// print the characters
[Link](#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)
[Link]();
i!(bis"=null)
[Link]();
+
+
+
//Example for the DataInput Stream (Byte Stream)
import [Link]..ata3nputStream;
import [Link]..ata4utputStream;
import [Link].,ile3nputStream;
import [Link].,ile4utputStream;
import [Link].34$0ception;
import [Link].3nputStream;
public class .[Link] {
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><@.==>[Link]+;

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([Link]();5)
{
// read character
double c = [Link]();
// print
[Link](c ) # #);
+
+catch($0ception e){
// i! any 3/4 error occurs
e.printStac12race();
+!inally{

// releases all system resources !rom the streams
i!(is"=null)
[Link]();
// i!(dos"=null)
// [Link]();
i!(dis"=null)
[Link]();
// i!(!os"=null)
// ![Link]();
+
+
+
//Example for the ByteArrayInput Stream (Byte Stream)
import [Link].8yteDrray3nputStream;
import [Link].34$0ception;
public class [Link] {
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 = [Link]())"=9:)
{ // convert byte to character
char c = (char)b;
[Link](#byte '#)b)#; char ' #) c);
+
[Link]([Link]())# -eached the end#);
+catch($0ception e){
// i! 3/4 error occurs
e.printStac12race();
+!inally{
i!(bais"=null)
[Link]();
+
+
+
//Example for FileOutputStream
import [Link].34$0ception;
import [Link].,ile4utputStream;
public class ,[Link] {
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
![Link](bs);
+catch($0ception e0){
// i! any error occurs
e0.printStac12race();
+!inally{
// releases all system resources !rom the streams
i!(!out"=null)
![Link]();
+
+
+
//Example for FilterOututStream
import [Link].,ile3nputStream;
import [Link].,ile4utputStream;
import [Link].,ilter4utputStream;
import [Link].34$0ception;
import [Link].4utputStream;
public class ,[Link] {
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
![Link](in!o);
// !orces byte contents to ritten out to the stream
!os.!lush();
+catch(34$0ception e){

// i! any 3/4 error occurs
[Link](#Close() is invo1ed prior to rite()#);
+!inally{

// releases any system resources associated ith the stream
i!(os"=null)
[Link]();
i!(!os"=null)
![Link]();
+
+
+
//Example for BufferedOututStream
import [Link].8u!!ered4utputStream;
import [Link].8yteDrray4utputStream;
import [Link].34$0ception;
public class 8u!![Link] {
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
[Link](b);
// !orce the byte to be ritten to baos
bos.!lush();
// convert 8yteDrray4utputStream to bytes
byte[] bytes = baos.to8yteDrray();
// prints the byte
[Link](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)
[Link]();
i!(bos"=null)
[Link]();
+
+
+
//Example for DataOututStream
import [Link]..ata3nputStream;
import [Link]..ata4utputStream;
import [Link].,ile3nputStream;
import [Link].,ile4utputStream;
import [Link].34$0ception;
import [Link].3nputStream;
public class .[Link] {
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><@.==>[Link]+;
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
[Link](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([Link]();5)
{
// read character
double c = [Link]();
// print
[Link](c ) # #);
+
+catch($0ception e){ // i! an 3/4 error occurs
e.printStac12race();
+!inally{ // releases all system resources !rom the streams
i!(is"=null)
[Link]();
i!(dos"=null)
[Link]();
i!(dis"=null)
[Link]();
i!(!os"=null)
![Link]();
+
+
+
//Example for rintStream
import [Link].F;
public class &[Link] {
public static void main(String[] args) {
int 0 = =;
// create printstream object
&rintStream ps = ne &rintStream([Link]);
// print integer
[Link](0);
[Link]();
[Link](:55);
// !lush the stream
// ps.!lush();
+
+
//Example for ByteArrayOututStream
import [Link].8yteDrray4utputStream;
import [Link].34$0ception;
public class [Link] {
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
[Link](<=);
// converts the byte to the de!ault charset value
str = [Link]();
// prints the string
[Link](str);
+catch($0ception e){
// i! 3/4 error occurs
e.printStac12race();
+!inally{
i!(baos"=null)
[Link]();
+
+
+
//Example for File!eader (Character Stream)
import [Link].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= ![Link]())"=9:){
[Link]((char) d);
+
F/
int charsread = ![Link](data);
[Link](#Characters read #)charsread);
[Link](ne String(data> 5 > charsread));
![Link]();
+
+
//Example for InputStream!eader (Character Stream)
import [Link].,ile3nputStream;
import [Link].34$0ception;
import [Link].3nputStream-eader;
public class [Link] {
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=[Link]())"=9:)
{
// int to character
c=(char)i;
[Link](#Character -ead' #)c);
+
+ catch ($0ception e) {

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

// closes the stream and releases resources associated
i!(!is"=null)
![Link]();
i!(isr"=null)
[Link]();
+
+
+
//Example for Buffered!eader (Character Stream)
import [Link].8u!!ered-eader;
import [Link].,ile3nputStream;
import [Link].3nputStream;
import [Link].3nputStream-eader;
public class 8u!![Link] {
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 = [Link]()) "= 9:)
{
// converts int to character
char c = (char)value;
// prints character
[Link](c);
+

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

// releases resources associated ith the streams
i!(is"=null)
[Link]();
i!(isr"=null)
[Link]();
i!(br"=null)
[Link]();
+
+
+
//Example for Strin"!eader
import [Link].F;
public class [Link] {
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) [Link]();
[Link](## ) c);
+
// close the stream
[Link]();
+ catch (34$0ception e0) {
e0.printStac12race();
+
+
+
//Example for OutputStream#riter
import [Link].F;
public class [Link] {
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
[Link](A5);
[Link](A:);
[Link](AB);
// !lush the stream
riter.!lush();
// read hat e rite
!or (int i = 5; i I C; i))) {
[Link](## ) (char) [Link]());
+
+ catch ($0ception e0) {
e0.printStac12race();
+
+
+
//Example for File#riter
import [Link].,ilterHriter;
import [Link];
import [Link];
public class ,[Link] {
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
[Link](#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 [Link].8u!!eredHriter;
import [Link].34$0ception;
import [Link];
public class 8u!![Link] {
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)))
{
[Link](i);
+
// !orces out the characters to string riter
b.!lush();
// string bu!!er is created
String8u!!er sb = s.get8u!!er();
// prints the string
[Link](sb);
+catch(34$0ception e){
// i! 3/4 error occurs
e.printStac12race();
+!inally{
// releases any system resources associated ith the stream
i!(s"=null)
[Link]();
i!(b"=null)
[Link]();
+
+
+
//Example for Strin"#riter
import [Link].F;
public class [Link] {
public static void main(String[] args) {
// create a ne riter
StringHriter s = ne StringHriter();
// rite integers that correspond to ascii code
[Link](A5);
[Link](A<);
// print result by converting to string
[Link](## ) [Link]());
+
+
//Example for O$%ect Seriali&ation usin" O$%ectInputStream and O$%ect Out Stream
//student'%a(a
import [Link];
class student implements SerialiJable{
public String name;
public int roll7umber;
public String Kuali!ication;
+
//!ecord)"mnt'%a(a
import [Link].F;
class -ecord(gmnt{
student st = null;
,ile4utputStream out;
4bject4utputStream o4ut;
public void initialiJaStudent3n!o(String name> int rn> String Kl){
st = ne student();
[Link] = name;
st.roll7umber = rn;
[Link]!ication = Kl;
+
public void [Link]()
{
try{
out = ne ,ile4utputStream(#students3n![Link]#);
o4ut = ne 4bject4utputStream(out);
initialiJaStudent3n!o(#abc#> :55> #(2ech#);
o4ut.rite4bject(st);
[Link](st);
initialiJaStudent3n!o(#de!#> :5:> #8$#);
o4ut.rite4bject(st);
[Link](st);
[Link](#Save in!o success!ully#);
[Link]();
[Link]();
+catch($0ception tt){+
+
public void [Link]()
{
try{
,ile3nputStream in = ne ,ile3nputStream(#students3n![Link]#);
4bject3nputStream i3n = ne 4bject3nputStream(in);
4bject obj = null;
hile (true)
{
obj = i3n.read4bject();
i!(obj == null)
brea1;
else{
st = (student) obj;
[Link]([Link])#99999#)st.roll7umber);
+
[Link](#999999999#);
+
[Link]();
[Link]();
+
catch($4,$0ception ee!){+
catch($0ception tt){ tt.printStac12race();+
+
public static void main(String a[]){
-ecord(gmnt rm = ne -ecord(gmnt();
[Link]();
[Link]();
+
+
//Example for Stream*o+eni&er
import [Link].F;
public class [Link] {
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,'
[Link](#$nd o! ,ile encountered.#);
eo! = true;
brea1;
case Stream2o1eniJer.22M$4%'
[Link](#$nd o! %ine encountered.#);
brea1;
case Stream2o1eniJer.22MH4-.'
[Link](#Hord' # ) [Link]);
brea1;
case Stream2o1eniJer.22M7L(8$-'
[Link](#7umber' # ) [Link]);
brea1;
de!ault'
[Link]((char) to1en ) # encountered.#);
i! (to1en == N"N) {
eo! = true;
+
+
+ hile ("eo!);
+ catch ($0ception e0) {
e0.printStac12race();
+
+
+

You might also like