6-File handling-13-11-2024 (1)
6-File handling-13-11-2024 (1)
6-File handling-13-11-2024 (1)
Java IO Packages
Introduction
• Java I/O (Input and Output) is used to process
the input and produce the output based on the
input.
• Java uses the concept of stream to make I/O
operation fast. The java.io package contains all
the classes required for input and output
operations.
• We can perform file handling in java by java
IO API.
Stream
• A stream is a sequence of data. In Java a stream is
composed of bytes. It's called a stream because it's
like a stream of water that continues to flow.
• In java, 3 streams are created for us automatically.
All these streams are attached with console.
• 1) System.out: standard output stream
• 2) System.in: standard input stream
• 3) System.err: standard error stream
Output Stream& Input stream
• Java application uses an output stream to write
data to a destination, it may be a file, an array,
peripheral device or socket.
Method Description
1) public void write(int)throws is used to write a byte to the current
IOException output stream.
2) public void write(byte[])throws is used to write an array of byte to the
IOException current output stream.
3) public void flush()throws IOException flushes the current output stream.
is used to close the current output
4) public void close()throws IOException
stream.
InputStream
•
Class
InputStream class is an abstract class. It is the
superclass of all classes representing an input
stream of bytes.
Method Description
reads the next byte of data from the
1) public abstract int read()throws
input stream. It returns -1 at the end of
IOException
file.
returns an estimate of the number of
2) public int available()throws
bytes that can be read from the current
IOException
input stream.
is used to close the current input
3) public void close()throws IOException
stream.
Java File Class
int read(byte[] b, int off, int len) It is used to read up to len bytes of data from
the input stream.
• programming");
• data.flush();
• data.close();
• System.out.println("Success...");
• }
• }
Buffered Stream
• Java BufferedInputStream Class
• Java BufferedInputStream class is used to read information from stream. It
internally uses buffer mechanism to make the performance fast.
The important points about BufferedInputStream are:
• When the bytes from the stream are skipped or read, the internal buffer
automatically refilled from the contained input stream, many bytes at a
time.
• When a BufferedInputStream is created, an internal buffer array is created.
• ("c://users/admin/desktop/java/one.txt");
• BufferedWriter buffer = new BufferedWriter(writer);
• buffer.write("Welcome to java File handling
• programming.");
• buffer.close();
• System.out.println("Success");
• }
• }
Character streams
• Java Byte streams are used to perform input and output of 8-bit bytes,
whereas Java Character streams are used to perform input and output
for 16-bit unicode. Though there are many classes related to character
streams but the most frequently used classes are, FileReader and
FileReader reads two bytes at a time and FileWriter writes two bytes at
a time.
Java FileWriter and FileReader
• Java FileWriter and FileReader classes are
used to write and read data from text files.
These are character-oriented classes, used for
file handling in java.
Java FileWriter class
• Java FileWriter class is used to write character-
oriented data to the file.
Constructors of FileWriter class:
• FileWriter(String file) :creates a new file. It
gets file name in string.
• FileWriter(File file) :creates a new file. It
gets file name in File object.
Methods of FileWriter
Methods Description
returns a character in
1) public int read() ASCII form. It returns -
1 at the end of file.
• import java.io.*;
• public class charstream { int c;
while ((c = in.read()) != -
• public static void main(String
1) {
args[]) throws out.write(c);
}
}finally {
• IOException {
if (in != null) {
• FileReader in = null; in.close();
• FileWriter out = null; }
if (out != null) {
• try { out.close();
• in = new }
FileReader("Sample.txt"); }
• out = new }
FileWriter("Sample2.txt"); }
•
• Similarly go through the rest all Classes such as
• CharArrayReader
• CharArrayWriter
• PrintStream
• PrintWriter
• OutputStreamWriter
• InputStreamReader
• PushbackInputStream
• PushbackReader
• StringWriter
• StringReader
• PipedWriter
• PipedReader
• FilterWriter
• FilterReader
Serialization and Deserialization in Java
import java.io.Serializable;
public class Student implements Serializable{
int id;
String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
}
• import java.io.*;
• class Student implements Serializable{
• int id;
• String name;
• public Student(int id, String name) {
• this.id = id;
• this.name = name;
• }
• }
• class serial
• {
• public static void main(String args[])throws Exception
• {
• Student s1 =new Student(211,"ravi");
• FileOutputStream fout=new FileOutputStream("f.txt");
• ObjectOutputStream out=new ObjectOutputStream(fout);
• out.writeObject(s1);
• out.flush();
• System.out.println("success");
• }
• }
Deserialization in java
• FileInputStream("f.txt"));
• Student s=(Student)in.readObject();
• System.out.println(s.id+" "+s.name);
• in.close();
• }
• }
//program using serialziation and deserialziation with array of objects
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Scanner;
public class serialdeserial
{
public static void main(String ar[])throws IOException,ClassNotFoundException
{
studentdemo sd[] = new studentdemo[3];
System.out.println("enter the details");
Scanner s =new Scanner(System.in);
String name,course;
ObjectOutputStream objout = new ObjectOutputStream(new FileOutputStream("student.txt"));
for(int i=0;i<sd.length;i++)
{
System.out.println("Enter name and course");
name= s.nextLine();
course=s.nextLine();
sd[i]= new studentdemo(name,course);
objout.writeObject(sd[i]);
}
objout.writeObject(new reading());
objout.close();
ObjectInputStream objin = new ObjectInputStream(new FileInputStream("student.txt"));
Object obj=null;
System.out.println("the data from the file -deserialization");
• while((obj=objin.readObject()) instanceof reading ==false)
• {
• System.out.println(((studentdemo)obj).name+" "+((studentdemo)obj).course);
• }
• objin.close();
• }
•
• }