Showing posts with label JAVA I/O. Show all posts
Showing posts with label JAVA I/O. Show all posts

Serialization in A IS-A relationship

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class SerilizableISA {

public static void main(String[] args) {
System.out.println("==========Serilization Started============");
try {
//creating object of Student class
Student1 s1 = new Student1(123, "Vijay Dehraj", "Java", 50000);
//creating stream to write data into the file
FileOutputStream fos = new FileOutputStream("inheritance.txt");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(s1);
//closing the stream
out.close();
System.out.println("The state of object is saved");
System.out.println("==========Serilization Ended==============");
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("==========Deserilization Started==========");
try {
//creating input stream to read the data of serialized object
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("inheritance.txt")
);
Student1 s = (Student1) ois.readObject();
//printing data of serialized object
System.out.println(s.id + " " + s.name + " " + s.course + " " + s.fee);
System.out.println("==========Deserilization Started==========");
} catch (Exception e) {
System.out.println(e);
}
}
}

Transient

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

class TransientDemo {

public static void main(String[] args) {
try {
//creating object of class Girl;
Girl g1 = new Girl("Bhaviya", 2345, 29);
//creating stram to write data;
FileOutputStream fos = new FileOutputStream("girl.txt");
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(g1);
out.flush();
//closing stream
out.close();
fos.close();
System.out.println("Write Operation Successful");
} catch (Exception e) {
System.out.println(e);
}
//deserializaton.................
try {
//creating stream to read data;
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("girl.txt")
);
//reading data
Girl g1 = (Girl) ois.readObject();
//g1.age =0 as age is transient;
//printing the data of serilized object
System.out.println(g1.name + " " + g1.id + " " + g1.age);
ois.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

Deserialization

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

class DeserializationDemo {

public static void main(String[] args)
throws IOException, FileNotFoundException, ClassNotFoundException {
//creating stream to read the object
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("student.txt")
);
Student s = (Student) ois.readObject();

//printing the data of serialized object
System.out.println(s.id + " " + s.name);
//closing the stream
ois.close();
}
}

Serialization

 

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

class SerializationDemo {
public static void main(String[] args) {
try {
Student s1 = new Student(23, "Ramakesh");
FileOutputStream fout = new FileOutputStream("student.txt");
ObjectOutputStream out = new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
// closing the stream
out.close();
System.out.println("State of object is successfully saved");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Writing Data to a file using and on console using PrintWriter

 

// Java PrintWriter class is the implementation of Writer class.
// It is used to print the formatted representation of objects
// to the text-output stream.

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

class PrintWriterDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
PrintWriter writer = new PrintWriter(System.out);
//Writing on console
writer.write("Java Invent");
writer.flush();
writer.close();

//writing in a file using printStream
PrintWriter writer1 = new PrintWriter("printwriter.txt");
writer1.write("welcome to Java Invent");
System.out.println("Success....");
writer1.flush();
writer1.close();
}
}

Reading data from a file using BufferedInputStream

 

// Java BufferedOutputStream class is used
// for buffering an output stream.
// It internally uses buffer to store data.
// It adds more efficiency than to write data directly into a stream.
// So, it makes the performance fast.

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class BufferedInputStreamDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("BufferedString.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int data;
while ((data = bis.read()) != -1) {
System.out.print((char) data);
}
System.out.println();
bis.close();
fis.close();
}
}

Reading data from Console using BufferedWriter

 

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

class ReadDataFromConsoleUsingBufferedWriter {

public static void main(String[] args)
throws FileNotFoundException, IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = "";
while (!str.equals("stop")) {
System.out.println("Enter data");
br.readLine();
System.out.println("Entered Data is : " + str);
}
br.close();
isr.close();
}
}

Reading data from a file using BufferedReader

 

// Java BufferedReader class is used to read the text
// from a character-based input stream.
// It can be used to read data line by line by readLine() method.
// It makes the performance fast. It inherits Reader class.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

class BufferedReaderDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileReader fr = new FileReader("BufferedWriter.txt");
BufferedReader br = new BufferedReader(fr);
int data;
while ((data = br.read()) != -1) {
System.out.print((char) data);
}
System.out.println();
br.close();
}
}

Writing Data Using BufferedWriter.

 

// Java BufferedWriter class is used to provide buffering
// for Writer instances.
// It makes the performance fast.
// It inherits Writer class.
// The buffering characters are used for providing
// the efficient writing of single arrays, characters, and strings.

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOError;
import java.io.IOException;

class JavaBufferedWriterDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileWriter fw = new FileWriter("BufferedWriter.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Using BufferedWriter to write a string");
System.out.println("writing operation executed successfully");
bw.close();
}
}

Reading data from a file using FileReader


// Java FileReader class is used to read data from the file.
// It returns data in byte format like FileInputStream class.
// It is character-oriented class which is used for file handling in java.

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

class JavaFileReaderDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileReader fr = new FileReader("writer.txt");
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
System.out.println();
fr.close();
}
}

Writing Data into a file using FileWriter

 

// Java FileWriter class is used to write character-oriented data to a file.
// It is character-oriented class which is used for file handling in java.

// Unlike FileOutputStream class,
// you don't need to convert string into byte array
// because it provides method to write string directly.

import java.io.FileWriter;

class JavaFileWriterDemo {

public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("filewriter.txt");
fw.write("File writer write char string directly");
System.out.println("Write operation executed succesfully");
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

Reading String from Console

 

import java.io.Console;

class ReadStringFromConsole {

public static void main(String[] args) {
Console c = System.console();
System.out.println("Enter your name");
String s = c.readLine();
System.out.println("Java Invent welcomes you " + s);
}
}

Reading password from console

 

import java.io.Console;

class ReadPasswordFromConsole {

public static void main(String[] args) {
Console c = System.console();
System.out.println("Enter the Password");
char[] ch = c.readPassword();
String password = String.valueOf(ch);
System.out.println("The password is : " + password);
}
}

Writing common data to multiple files using ByteArrayOutputStream


// Java ByteArrayOutputStream class is used to write common data into multiple files.
// In this stream, the data is written into a byte array which can be written to
// multiple streams later.
// The ByteArrayOutputStream holds a copy of data and forwards it to multiple streams.
// The buffer of ByteArrayOutputStream automatically grows according to data.

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

class ByteArrayOutputStreamDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileOutputStream fos1 = new FileOutputStream("a.txt");
FileOutputStream fos2 = new FileOutputStream("b.txt");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String s = "I am learning Input Output Stream";
byte b[] = s.getBytes();
baos.write(b);
baos.writeTo(fos1);
baos.writeTo(fos2);
baos.flush();
baos.close();
fos1.close();
fos2.close();
System.out.println("Data copied in both files Successfully");
}
}

Reading data from multiple files sequentially using SequenceInputStream

 

// Java SequenceInputStream class is used to read data from multiple streams.
// It reads data sequentially (one by one).

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.SequenceInputStream;

class SequenceInputStreamDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileInputStream fis1 = new FileInputStream("BufferedString.txt");
FileInputStream fis2 = new FileInputStream("String.txt");
SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
int data;
while ((data = sis.read()) != -1) {
System.out.print((char) data);
}
System.out.println();
sis.close();
fis1.close();
fis2.close();
}
}

Reading data from a file using BufferedReader

 

/* 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. */

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

class BufferedReaderDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileReader fr = new FileReader("BufferedWriter.txt");
BufferedReader br = new BufferedReader(fr);
int data;
while ((data = br.read()) != -1) {
System.out.print((char) data);
}
System.out.println();
br.close();
}
}

Writing data to a file using BufferedWriter


/* Java BufferedOutputStream class is used for buffering an output stream.
It internally uses buffer to store data.
It adds more efficiency than to write data directly into a stream.
, it makes the performance fast.*/

import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOError;
import java.io.IOException;

class JavaBufferedWriterDemo {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileWriter fw = new FileWriter("BufferedWriter.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Using BufferedWriter to write a string");
System.out.println("writing operation executed successfully");
bw.close();
}
}

Reading data from a file using FileInputStream

 

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

class FISDemo2 {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("abc.txt");
int data = 0;
while ((data = fis.read()) != -1) {
System.out.println((char) data);
}
fis.close();
}
}

Taking sourcefile and destination file address from User to read and write data using FileInputStream and FileOutputStream


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileCopy2 {

public static void main(String[] args)
throws FileNotFoundException, IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Source File");
String srcFile = sc.nextLine();
System.out.println("Enter Destination File");
String destFile = sc.nextLine();
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
int data;
while ((data = fis.read()) != -1) {
fos.write(data);
}
System.out.println("File copied");
}
}

 

Writing data to a file using FileOutputStream

 

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

class FOSDemo1 {

public static void main(String[] args)
throws FileNotFoundException, IOException {
FileOutputStream fos = new FileOutputStream("bbc.txt");
fos.write(10);
System.out.println("Data has been saved");
fos.close();
}
}

Google Script for Data Entry Form in Google Spreadsheet

// function to validate the entry made by user in user form function validateEntry (){ // declare a variable and referernece of active goog...