💻Step 23:I/O package 18th+19th hour+code

💡Definition-

It’s an API of Java which is used to read and write the data.

💡Stream

It’s a mediator which connects our program to input and output device.
It’s a continuous flow of data which travel between source and target.

💡Types of streams-

💡1.Input Stream:

Input stream is mediator between the Java program and the Input devices like keyboard, files.

💡2.Output Stream:

Output stream is mediator between the Java program and the Output devices like Monitor, network.

💡Stream Classes-

💡Byte Stream classes:

These classes works with 8 bit data.
and support ASCII code systems.

💡Character Stream classes:

These classes work with 16 bit dara and support UNICODE code systems.

Note-

The input streams of byte stream classes are similar to the Reader streams of character stream classes
and the output streams are equivalent to Writer streams of character stream classes.

💡ByteStream classes-

bsc

💡-Character Stream Class-

csc

code samples are here

1- Fileinputstream and Fileoutputstream

package byte1;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FOSandFIS {
public static void main(String[] args) throws IOException {

FileOutputStream fileOutputStream = new FileOutputStream("C:\\kittu8.txt");
String a = "hi i am mr.java";

byte b[] = a.getBytes();

fileOutputStream.write(b);

FileInputStream fis = new FileInputStream("C:\\kittu8.txt");

byte[] b1 = new byte[fis.available()];

int i = 0;
while ((i = fis.read()) != -1) {
System.out.print((char) i);
}
}
}
 
2- ByteArrayOutputstream and ByteArrayInputstream
 

package byte1;

import java.io.*;

public class BAOSandBAIS {

public static void main(String[] args) throws IOException {

ByteArrayOutputStream bao = new ByteArrayOutputStream();

String a = "abcdefg";

byte[] b = a.getBytes();

bao.write(b);

bao.writeTo(new FileOutputStream("C:\\kittu324.txt"));

bao.writeTo(new FileOutputStream("C:\\kittu718.txt"));

FileInputStream inputStream = new FileInputStream("C:\\kittu718.txt");

byte[] b2 = new byte[inputStream.available()];

inputStream.read(b2);

ByteArrayInputStream ais = new ByteArrayInputStream(b2);

int i;
while ((i = ais.read()) != -1) {
System.out.print((char) i);
}
}

3-DataOutputstream and DataInputstream

package byte1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DOSandDIS {

public static void main(String[] args) throws IOException {

DataOutputStream outputStream=new DataOutputStream(new FileOutputStream("D://titu7.txt"));

outputStream.writeInt(123);

DataInputStream stream=new DataInputStream(new FileInputStream("D://titu7.txt"));

System.out.println(stream.readInt());

}

}

4-BufferedOutputstream and BufferedInputstream

package byte1;

import java.io.*;

public class BFOSandBFIS {

public static void main(String[] args) throws IOException {

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/FileIO.txt"));

String str = "BufferedOutputStreamed Example";

byte[] b = str.getBytes();

bos.write(b);

bos.close();

BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("D:/FileIO.txt"));

int i = 0;
while ((i = inputStream.read()) != -1) {
System.out.print((char) i);
}

}
}

5-PrintStream

package byte1;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.io.PrintWriter;

public class ExamplePrintStream {

public static void main(String[] args) throws FileNotFoundException {
PrintStream stream = new PrintStream("D://fg.txt");
stream.print("hey");

PrintWriter writer = new PrintWriter("d://gf.txt");
writer.print("bye");
writer.close();
}

}

💡Serialization and De-Serialization in java-

💡Serialization- It’s a process of converting an object into file.

💡De-Serialization- It’s a process of converting an file into object.

💡Serializable interface –

It’s a marker interface.

An empty interface without any methods is known as marker interface. It’s used to indicate to perform any special action.

6-ObjectOutputStream and ObjectInputStream (Serialization and De-Serialization)

(i)ObjectOutputStream (Serialization) –

package byte1;

import java.io.*;

public class PersonSER implements Serializable {

private int age;

private String name;

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static void main(String[] args) throws FileNotFoundException, IOException {

PersonSER emp = new PersonSER();
emp.setName("amit");
emp.setAge(10);

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d://cf10.txt"));
oos.writeObject(emp);

}

}
(ii)ObjectInputStream De-Serialization

package byte1;

import java.io.*;

public class PersonDSER {

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {

ObjectInputStream is = new ObjectInputStream(new FileInputStream("d://cf10.txt"));

Object pl = is.readObject();

PersonSER tr1 = (PersonSER) pl;

System.out.println(tr1.getName());
System.out.println(tr1.getAge());

}
}

1-FileReader and FileWriter


package byte1;

import java.io.*;

public class FWandFR {

public static void main(String[] args) throws IOException {

FileWriter fw = new FileWriter("D:\\titu78.txt");
String a = "hi i am java";

fw.write(a);

fw.flush();

FileReader fr = new FileReader("D:\\titu78.txt");

int i = 0;
while ((i = fr.read()) != -1) {
System.out.print((char) i);
}

}

}

2- CharArrayReader and CharArrayWriter


package byte1;

import java.io.*;

public class CAWandCAR {

public static void main(String[] args) throws IOException {

CharArrayWriter caw = new CharArrayWriter();

String msg = "i am java guy";

caw.write(msg);

FileWriter writer = new FileWriter("D://gyh.txt");
FileWriter writer1 = new FileWriter("D://hgk.txt");

caw.writeTo(writer);
caw.writeTo(writer1);

writer.flush();
writer1.flush();
caw.flush();

String j = "fhgfjgfjhgfgh";
char[] c = j.toCharArray();

CharArrayReader car = new CharArrayReader(c);
int i = 0;
while ((i = car.read()) != -1) {
System.out.print((char) i);
}
}

}

3-BufferedReader and BufferedWriter


package byte1;

import java.io.*;

public class BFWandBFR {

public static void main(String[] args) throws IOException {

BufferedWriter bfw = new BufferedWriter(new FileWriter("D:\\titu190.txt"));

String msg = "i am awesome!";
bfw.write(msg);
bfw.flush();

BufferedReader bfr = new BufferedReader(new FileReader("D:\\titu19q0.txt"));

String msg1 = bfr.readLine();
System.out.println(msg1);
}

}

4-  PrintWriter


package byte1;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class ExPrintwriter {

public static void main(String[] args) throws FileNotFoundException {
PrintWriter p = new PrintWriter("D://fg.txt");
p.println("i am java lover");
}

}

Interview Questions —

  • What is stream in java?
  • What is difference between character stream and byte stream class?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.