package io.
pkg;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
public class BufferedInputStreamDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
FileInputStream fin=new FileInputStream("file1.txt");
BufferedInputStream bin=new BufferedInputStream(fin);
int i;
while((i=bin.read())!=-1){
System.out.print((char)i);
bin.close();
fin.close();
}catch(Exception e){System.out.println(e);}
//
==============================================
==============================================
===
package io.pkg;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class BufferedOutputStreamDemo {
public static void main(String args[])throws Exception{
FileOutputStream fout=new FileOutputStream("file1.txt");
BufferedOutputStream bout=new BufferedOutputStream(fout);
String s="Working with BufferedOutputStream...";
byte b[]=s.getBytes();
bout.write(b);
bout.flush();
bout.close();
fout.close();
System.out.println("success");
//
==============================================
==============================================
===
package io.pkg;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileReader fr = new FileReader("file1.txt");
BufferedReader br=new BufferedReader(fr);
//br.readLine();
int i;
while((i=br.read())!=-1){
System.out.print((char)i);
br.close();
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
==============================================
==============================================
===
package io.pkg;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileWriter writer;
try {
writer = new FileWriter("file1.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Working on BufferedWriter...");
buffer.close();
System.out.println("Success");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /*
try {
FileReader fr = new FileReader("file1.txt");
BufferedReader br=new BufferedReader(fr);
//br.readLine();
int i;
while((i=br.read())!=-1){
System.out.print((char)i);
br.close();
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
*/
//
==============================================
==============================================
===
package io.pkg;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream input;
try {
input = new FileInputStream("file3.txt");
DataInputStream inst = new DataInputStream(input);
double a = inst.readDouble();
int b = inst.readInt();
boolean c = inst.readBoolean();
char d = inst.readChar();
System.out.println("Values: a: " + a + " b:" + b + "
c:" + c + " d:" + d);
inst.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//
==============================================
==============================================
===
package io.pkg;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileOutputStream file = new FileOutputStream("file3.txt");
DataOutputStream data = new DataOutputStream(file);
data.writeDouble(1.1);
data.writeInt(55);
data.writeBoolean(true);
data.writeChar('4');
data.flush();
data.close();
System.out.println("Succcess...");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//
==============================================
==============================================
===
package io.pkg;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
File f= new File("file1.txt");
/* try {
if(f.createNewFile())
System.out.println("File created successfully...");
else
System.out.println("Error in creating the file..");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
/* FileWriter fw;
try {
fw = new FileWriter(f, true);
fw.write("Hello!");
fw.flush();
fw.close();
System.out.println("File write done!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
try {
FileReader fr= new FileReader(f);
char c[]= new char[100];
fr.read(c);// copy the file content of max 100 chars into c
System.out.println(c);// display the content of file via c
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//
==============================================
==============================================
===
package io.pkg;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileInputStreamDemo {
public static void main(String args[]){
FileInputStream fin;
try {
fin = new FileInputStream("file1.txt");
// int i=fin.read();
// System.out.print((char)i);
int i;
while((i=fin.read())!=-1){
System.out.print((char)i);
fin.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
==============================================
==============================================
===
package io.pkg;
import java.io.FileOutputStream;
public class FileOutputStreamExample {
public static void main(String args[]){
try{
FileOutputStream fout=new FileOutputStream("file1.txt", true);
String msg="Learning Java io package..";
byte b[]= msg.getBytes();
fout.write(b, 9, 15); //ASCII char A is written in the file.
fout.flush();
fout.close();
System.out.println("success...");
}catch(Exception e){System.out.println(e);}
//
==============================================
==============================================
===
package io.pkg;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr;
try {
fr = new FileReader("file1.txt");
int i;
while((i=fr.read())!=-1)
System.out.print((char)i);
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
==============================================
==============================================
===
package io.pkg;
import java.io.FileWriter;
public class FileWriterDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileWriter fw=new FileWriter("file1.txt");
fw.write("Learning Java programming.");
fw.close();
}catch(Exception e){System.out.println(e);}
System.out.println("Success...");
//
==============================================
==============================================
===
package io.pkg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStreamReader isr= new InputStreamReader(System.in);
int i;
try {
System.out.println("Enter a line: ");
/*
char c;
while((i=isr.read())!=-1) {
c=(char)i;
System.out.print(c);
}*/
BufferedReader br= new BufferedReader(isr);
String msg= br.readLine();
System.out.println(msg);
br.close();
isr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//
==============================================
==============================================
===
package io.pkg;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class IODemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileInputStream f;
try {
f = new FileInputStream("./myFile.txt");
BufferedReader br= new BufferedReader(new
InputStreamReader(f));
//System.out.println("Enter your name: ");
String stmt= br.readLine();
System.out.println(stmt);
br.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//
==============================================
==============================================
===
package io.pkg;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class PrintWriterDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintWriter writer = new PrintWriter(System.out);
writer.write("Learining PrintWriter Class...");
writer.flush();
writer.close();
//Data to write in File using PrintWriter
PrintWriter writer1 =null;
try {
writer1 = new PrintWriter(new File("file4.txt"));
writer1.write("Java applications...");
writer1.flush();
writer1.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//
==============================================
==============================================
===
package io.pkg;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileExample {
static final String FILEPATH ="file2.txt";
public static void main(String[] args) {
try {
writeToFile(FILEPATH,"Abc PQR...",1);
System.out.println(new String(readFromFile(FILEPATH, 0, 50)));
writeToFile(FILEPATH, "We are working with RandomAccessFile", 31);
System.out.println(new String(readFromFile(FILEPATH, 0, 50)));
} catch (IOException e) {
e.printStackTrace();
private static byte[] readFromFile(String filePath, int position, int size)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "r");
file.seek(position);
byte[] bytes = new byte[size];
file.read(bytes);
file.close();
return bytes;
private static void writeToFile(String filePath, String data, int position)
throws IOException {
RandomAccessFile file = new RandomAccessFile(filePath, "rw");
file.seek(position);
file.write(data.getBytes());
file.close();
}
//
==============================================
==============================================
===
package io.pkg;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Employee1 implements Serializable{
private int id;
private String name;
private float salary;
public int getId() {
return id;
public void setId(int id) {
this.id = id;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public float getSalary() {
return salary;
public void setSalary(float salary) {
this.salary = salary;
public Employee1(int id, String name, float salary) {
super();
this.id = id;
this.name = name;
this.salary = salary;
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary="
+ salary + "]";
public class SerializableDemo {
public static void main(String[] args) {
try {
FileOutputStream fout=new FileOutputStream("file1.txt");
ObjectOutputStream oos= new ObjectOutputStream(fout);
Employee1 emp= new Employee1(1001,"Amita",
555555.5f);
oos.writeObject(emp);
oos.flush();
oos.close();
fout.close();
System.out.println("Object write is done...");
FileInputStream fis = new FileInputStream("file1.txt");
ObjectInputStream ois= new ObjectInputStream(fis);
Employee1 e1= (Employee1)ois.readObject();
System.out.println("after reading the object from the file");
System.out.println("Id: "+e1.getId()+ " Name:
"+e1.getName()+" Salary: "+e1.getSalary());
ois.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//
==============================================
==============================================
===
package io.pkg;
import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
public class StreamTokenizerDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
StringReader sr = new StringReader ("Hi! this is 1 Java
Virtual 3 Guru from TechM");
StreamTokenizer st = new StreamTokenizer(sr);
st.resetSyntax();
st.wordChars('A', 'Z');
st.wordChars('a', 'z');
int type;
while ((type = st.nextToken ()) !=
StreamTokenizer.TT_EOF ){
if (type == StreamTokenizer.TT_WORD)
System.out.println(st.sval);
}catch (IOException e) {
System.err.println ("Error :" +e.getMessage());
//
==============================================
==============================================
===
package io.pkg;
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student1 implements Serializable, Externalizable{
private int rollNo;
private String name;
private float score;
public Student1() {}// mandatory while using Externalizable
public Student1(int rollNo, String name, float score) {
this.rollNo=rollNo;
this.name=name;
this.score=score;
public int getRollNo() {
return rollNo;
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
public float getScore() {
return score;
public void setScore(float score) {
this.score = score;
public String toString() {
return getRollNo()+" "+getName()+" "+getScore();
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
// TODO Auto-generated method stub
System.out.println("writeExternal()");
/*In the writeExternal() method, we're adding the object's
properties
* to the ObjectOutput stream. This has standard methods like
writeUTF()
* or writeObject for String and writeInt() for the int values.*/
//perform any operation before you write the object
out.writeInt(rollNo);
out.writeObject(name);
out.writeFloat(score);
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
// TODO Auto-generated method stub
System.out.println("readExternal()");
//1. for deserializing the object, we're reading from the
ObjectInput stream
//2. perform any operation before you write the object
//3. Ensure to read the properties in the same exact order in
which they were written
int rollNo= in.readInt();
String name= (String)in.readObject();
float score= in.readFloat();
System.out.println("RollNo: "+rollNo+" Name: "+name+" Score:
"+score);
public class StudentExternalizableDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student1 obj= new Student1();
obj.setRollNo(1);obj.setName("Sudha"); obj.setScore(96.9f);
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(new
FileOutputStream("file1.txt"));
obj.writeExternal(oos);
oos.flush();
oos.close();
System.out.println("Object is written in the file!");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
ObjectInputStream ois= new ObjectInputStream(new
FileInputStream("file1.txt"));
obj.readExternal(ois);
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//
==============================================
==============================================
===
package io.pkg;
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable{
private int rollNo;
private String name;
private float score;
public Student() {}
public Student(int rollNo, String name, float score) {
this.rollNo=rollNo;
this.name=name;
this.score=score;
public int getRollNo() {
return rollNo;
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
public String getName() {
return name;
public void setName(String name) {
this.name = name;
}
public float getScore() {
return score;
public void setScore(float score) {
this.score = score;
public String toString() {
return getRollNo()+" "+getName()+" "+getScore();
public class StudentSerializableDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Student obj= new Student();
obj.setRollNo(1);obj.setName("Sudha"); obj.setScore(96.9f);
ObjectOutputStream oos;
try {
oos = new ObjectOutputStream(new
FileOutputStream("file1.txt"));
oos.writeObject(obj);
oos.flush();
oos.close();
System.out.println("Object is written in the file!");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
try {
ObjectInputStream ois= new ObjectInputStream(new
FileInputStream("file1.txt"));
Student s=(Student)ois.readObject();
System.out.println(s);
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//
==============================================
==============================================
===