Chapter-8 - File and IO
Chapter-8 - File and IO
• Files
– Creating a file
– Writing to a file
– Reading from a file
• Object Serialization
• File Class
CSE Program 2
Input/output Basics ASTU
CSE Program 4
Contd. ASTU
CSE Program 6
Java Classes ASTU
CSE Program 7
Byte Streams ASTU
CSE Program 9
Example :BufferedOutputStream. ASTU
import java.io.*;
class Create2
{ public static void main(String args[]) throws IOException
{ //attach keyboard to DataInputStream
DataInputStream dis = new DataInputStream (System.in);
//attach file to FileOutputStream, if we use true then it will open in append mode
FileOutputStream fout = new FileOutputStream (“D:myfile", true);
BufferedOutputStream bout = new BufferedOutputStream (fout, 1024);
//Buffer size is declared as 1024 otherwise default buffer size of 512 bytes is used.
//read data from DataInputStream and write into FileOutputStream
char ch;
System.out.println ("Enter # at end : " ) ;
while ( (ch = (char) dis.read() ) != ‘#' )
bout.write (ch);
bout.close ();
fout.close ();
}
}
CSE Program 10
Example: BufferedInputStream ASTU
import java.io.*;
class Read2
{ public static void main(String args[]) throws IOException
{ //attach the file to FileInputStream
FileInputStream fin = new FileInputStream (“D:myfile");
BufferedInputStream bin = new BufferedInputStream (fin);
//read data from FileInputStream and display it on the monitor
int ch;
while ( (ch = bin.read() ) != -1 )
System.out.print ( (char) ch);
fin.close ();
}
}
CSE Program 11
Java SequenceInputStream Class ASTU
CSE Program 12
SequenceInputStream ASTU
import java.io.*;
class InputStreamExample {
public static void main(String args[])throws Exception{
FileInputStream input1=new FileInputStream("D:testin.txt");
FileInputStream input2=new FileInputStream("D:testout.txt");
SequenceInputStream inst=new SequenceInputStream(input1, input2);
int j;
while(( j=inst.read())!=-1){
System.out.print((char)j);
}
inst.close();
input1.close();
input2.close();
}
}
CSE Program 13
Java ByteArrayStream ASTU
CSE Program 14
Example of Java ByteArrayOutputStream ASTU
import java.io.*;
public class DataStreamExample {
public static void main(String args[])throws Exception{
FileOutputStream fout1=new FileOutputStream("D:\\f1.txt");
FileOutputStream fout2=new FileOutputStream("D:\\f2.txt");
bout.flush();
bout.close();//has no effect
System.out.println("Success...");
}
}
CSE Program 15
ByteArrayInputStream example ASTU
import java.io.*;
public class ReadExample {
public static void main(String[] args) throws IOException {
byte[] buf = { 35, 36, 37, 38 };
// Create the new byte array input stream
ByteArrayInputStream byt = new ByteArrayInputStream(buf);
int k = 0;
while ((k = byt.read()) != -1) {
//Conversion of a byte into character
char ch = (char) k;
System.out.println("ASCII value of Character is:" + k + "; Special character is: " + ch);
}
}
}
CSE Program 16
Byte Stream Classes ASTU
CSE Program 17
Character or Text Streams ASTU
CSE Program 18
Text Stream Classes ASTU
CSE Program 19
Example of Java BufferedWriter ASTU
import java.io.*;
public class BufferedWriterExample {
public static void main(String[] args) throws Exception {
FileWriter writer = new FileWriter("D:/testout.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Welcome to java IO Class.");
buffer.close();
System.out.println("Success");
}
}
CSE Program 20
Java BufferedReader Example ASTU
In this example, we are reading the data from the text file testout.txt using Java
BufferedReader class.
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D||testout.txt");
BufferedReader br=new BufferedReader(fr);
int i;
while((i=br.read())!=-1){
System.out.print((char)i);
}
br.close();
fr.close();
}
}
CSE Program 21
Java BufferedReader Example ASTU
In this example, we are reading the data from the text file testout.txt using Java
BufferedReader class.
import java.io.*;
public class BufferedReaderExample {
public static void main(String args[])throws Exception{
FileReader fr=new FileReader("D||testout.txt");
BufferedReader br=new BufferedReader(fr);
int i;
while((i=br.read())!=-1){
System.out.print((char)i);
}
br.close();
fr.close();
}
}
CSE Program 22
Java CharArrayReader Class ASTU
• The CharArrayReader is composed of two words: CharArray and Reader. The CharArrayReader
class is used to read character array as a reader (stream). It inherits Reader class.
import java.io.CharArrayReader;
public class CharArrayExample{
public static void main(String[] ag) throws Exception {
char[] ary = { 'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't' };
CharArrayReader reader = new CharArrayReader(ary);
int k = 0;
// Read until the end of a file
while ((k = reader.read()) != -1) {
char ch = (char) k;
System.out.print(ch + " : ");
System.out.println(k);
}
}
}
CSE Program 23
CharArrayWriterExample ASTU
import java.io.CharArrayWriter;
import java.io.FileWriter;
public class CharArrayWriterExample {
public static void main(String args[])throws Exception{
CharArrayWriter out=new CharArrayWriter();
out.write("Welcome to javaTpoint");
FileWriter f1=new FileWriter("D:||a.txt");
FileWriter f2=new FileWriter("D:||b.txt");
FileWriter f3=new FileWriter("D:||c.txt");
FileWriter f4=new FileWriter("D:||d.txt");
out.writeTo(f1);
out.writeTo(f2);
out.writeTo(f3);
out.writeTo(f4);
f1.close(); f2.close();
f3.close(); f4.close();
System.out.println("Success...");
}
24
} CSE Program
PrintWriter Example ASTU
import java.io.File;
import java.io.PrintWriter;
public class PrintWriterExample {
public static void main(String[] args) throws Exception {
//Data to write on Console using PrintWriter
PrintWriter writer = new PrintWriter(System.out);
writer.write("Javatpoint provides tutorials of all technology.");
writer.flush();
writer.close();
//Data to write in File using PrintWriter
PrintWriter writer1 =null;
writer1 = new PrintWriter(new File("D:/testout.txt"));
writer1.write("Like Java, Spring, Hibernate, Android, PHP etc.");
writer1.flush();
writer1.close();
}
} 25
CSE Program
File ASTU
CSE Program 26
Java FileInput /output Stream Class ASTU
• Java FileInputStream class obtains input bytes from a file. It is used for
reading byte-oriented data (streams of raw bytes) such as image data,
audio, video etc.
• You can also read/write character-stream data. But, for reading / writing
streams of characters, it is recommended to use FileReader class.
CSE Program 27
Object Serialization ASTU
import java.io.*;
import java.util.*;
class Employ implements Serializable
{ private int id;
private String name;
private float sal;
private Date doj;
Employ (int i, String n, float s, Date d)
{ id = i;
name = n;
sal = s;
doj = d;
}
void display ()
{
System.out.println (id+ "\t" + name + "\t" + sal + "\t" + doj);
}
CSE Program 34
ASTU
import java.io.*;
import java.util.*;
class StoreObj
{ public static void main (String args[]) throws IOException
{ BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
FileOutputStream fos = new FileOutputStream ("objfile");
ObjectOutputStream oos = new ObjectOutputStream ( fos );
System.out.print ("Enter how many objects : ");
int n = Integer.parseInt(br.readLine () );
for(int i = 0;i<n;i++)
{ Employ e1 = Employ.getData ();
oos.writeObject (e1);
}
oos.close ();
fos.close ();
}
}
CSE Program 36
Example ASTU
42