java i/o 

  • By
  • September 15, 2021
  • JAVA

let us understand someterminolgy:

persistence data:

  • the envirment that allow us to store data permanently is called persistence media.

so we can store data permanently in three places.

File

DataBase

Remote Computer (Socket)

  • persistence  is the process of storing data permanently in a persistence  media is called persistence.

persistence  Logic:

  • the logic that persist data in a persistence  media is called persistence  logic.

Ex:IOStream based Logic,JDBC basedLogic ,Networking Based Logic.

persistence  Technologis:

the technologi that provide API to develop persistence  logic is called persistence  technology.

IOStream : to persist data in flat files

JDBC  ,Hibernate:persist data in DB 

Networking : persist data in remote computer

 

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

  • where we can store data permanently?

storing data in variable and array in temporary. data will be lost when a local variable goes out of scope or when the programm terminate.

so thats why programmer use files or DB for long term storage of larg ammount of data. it will available even after termination of the programm. Java Training In Pune refer to maintain data on file as persistance  data,because  the data exist beyond the duration of programm execution.

 

IOStream API is given to store and read data from files

JDBC is given to store data and read data  from database.

 

java i/o 

java input output is used to process the input and produce the output.

stream concept is used to make i/o opration fast .

java.io package contain all classes requird for i/o operation.

stream

  • it is sequence of data.a stream is composed of bytes it’s called stream because it is like stream of  water that continue to flow.
  • InputStream: this depict the flow of byte from data source to the pogramm memory.
  • OutputStream : this depict the flow of bytes from the pogramm memory to destination data store .
  • java vies these stream in term of object that will perform different operation on the stream through their method calls.
  • two basic operations are:

Read from input stream.

Write to output stream.

 

1)System.out: standerd output stream

2)System.in:standerd input stream

3)System.err:statnderd error stream

we can print output and error message to the consol

System.out.println(“massge without error”);

System.out.println(“massage with error”);

we can get input from consol.

example:

int i=System.in.read(); 

System.out.println((char)i); // print the char

  • There different Type of stream:

InputStream and OutputStream

let understand from diagram:

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

InputStream:

It is an Abstract class that define method to performing input operation i.e. Read data from persistent media (source).

 

this class  the super class of all classess represanting an input stream of bytes.

application that need to define a subclass of InputStream must alwyas provide  a method that returns the next byte of input.

Set of all classes which extends the InputStream class

 

OutputStream 

this is an abstract class that define method  to perfom ouput operations i.e writing data to destination.

 

Abstract class is super class  of all classes representing an output stream byte.

application that need to define a subclass of outputStream must always provide a method that return the next byte of output.

 

Set of all classes which extends the InputStream class

 

Java FileInputStream Class

java FileInputStream  is used to abtain input bytes from file in file system. it is reading stream of row bytes such as images ,audio,vidio etc.

Declaration of FilesInputStream class 

public class FileInputStream extends InputStream

 

method:

FileInputStream  (File)

: create a FileInputStream  by opening a connection to actual file,the file named by the file object  file in the file system.

FileInputStream  (String name)

: create a FileInputStream  by opening a connection to atual file,the file named by the path name in the file system.

int read()

: Read a byte from this input stream.

 

Example1: read single Caracter

import java.io.FileInputStream;

 

import java.io.IOException;

 

public class fileInputStream {

 

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

FileInputStream f1=new FileInputStream(“F:<\\batch sahil\\java\\myfile.txt>);

int i=f1.read();

System.out.println((char)i);

f1.close();

}

}

 

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

——————————————————————————–

Example2: read All Caracter

 

import java.io.FileInputStream;

 

import java.io.IOException;

 

public class demo2 {

 

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

      

            FileInputStream f=new FileInputStream(“F:<\\batch sahil\\java\\myfile.txt>);    

            int i=0;    

            while((i=f.read())!=-1){    

            System.out.print((char)i);    

            }    

            f.close();    

             

        }    

 

}

 

————————————————————————————————

 

FileOutputStream class 

this is  an output  stream used for writing data to a file.

declaration of FileOutputStream:

public class FileOutputStream extends OutputStream

 

method :

1.FileOutputStream (File file)  

: creates a file output to write to file represented by the specified file object.

2.FileOutputStream(String name)

: Creates a file output stream to write to the file with the specified name.

  1. FileOutputStream(File file ,boolean append )

: Creates a file output stream to write to the file represented by specified File object.

4.write (int b) 

: writes the specified byte o this file output stream.

  1. close()

: close the FileOutputStream

example 1:write one char

import java.io.FileOutputStream;

import java.io.IOException;

 

public class demoFileWrite {

 

public static void main(String[] args) {

try {

FileOutputStream f1=new FileOutputStream(“F:<\\batch sahil\\java\\myfile.txt>);

int c=0;

f1.write(65);

f1.close();

System.out.println(“successfully execute…”);

} catch (IOException e) {

}

}

For Free, Demo classes Call: 8237077325
Registration Link: Click Here!

 

}

  —————————————————————————

example 2: write a string using byte array

 

import java.io.FileOutputStream;

import java.io.IOException;

 

public class demoFileWrite {

public static void main(String[] args) {

try {

FileOutputStream f1=new FileOutputStream(“F:<\\batch sahil\\java\\myfile.txt>”);

String s1=“welcome to my class”;

byte b1[]=s1.getBytes(); // converting string into byte array 

f1.write(b1);

               f1.close();

System.out.println(“successfully execute…”);

} catch (IOException e) {

}

}

}

————————————————————————————————

example 3:File copy

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

 

public class filecopy {

 

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

FileInputStream fr=new FileInputStream(“F:java\\myfile.txt);

FileOutputStream fw=new FileOutputStream(“F:java\\myfile11.txt);

int i;

while((i=fr.read())!=-1) {

fw.write(i);

}

}}

———————————————————

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.

public class ByteArrayOutputStream extends OutputStream  

 

import java.io.ByteArrayOutputStream;

 

import java.io.FileOutputStream;

import java.io.IOException;

 

public class byteArrDemo {

 

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

  FileOutputStream f1=new FileOutputStream(“F:java\\myfile.txt);    

      FileOutputStream f2=new FileOutputStream(“F:\\java\\myfile1.txt);    

        

      ByteArrayOutputStream b=new ByteArrayOutputStream();    

    b.write(68);    

    b.writeTo(f1);    

    b.writeTo(f2);    

        

 

    b.close();//has no effect    

      System.out.println(“Success…”);    

 

}

 

}

———————————————————————————-

ByteArrayInputStream 

The ByteArrayInputStream is composed of two words: ByteArray and InputStream. As the name suggests, it can be used to read byte array as input stream.

 

Java ByteArrayInputStream class contains an internal buffer which is used to read byte array as stream. In this stream, the data is read from a byte array.

 

The buffer of ByteArrayInputStream automatically grows according to data.

 

public class ByteArrayInputStream extends InputStream  

 

import java.io.ByteArrayInputStream;

import java.io.IOException;

 

public class byteinputArr {

 

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);  

    }  

  }  

}  

———————————————————————————————-

DataOutputStream 

Java DataOutputStream class allows an application to write primitive Java data types to the output stream in a machine-independent way.

 

Java application generally uses the data output stream to write data that can later be read by a data input stream.

DataOutputStream  have special method for reading  and writing data in primitive type size bytes.

DataOutputStream is a sub class of filterOutputStream and DataOutputStream

import java.io.DataOutputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

public class DataoutputStream {

 

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

FileOutputStream f=new FileOutputStream(“F:java\\myfile.txt”);

DataOutputStream d1=new DataOutputStream(f);

d1.writeInt(99);

d1.writeBytes(“ppppp”);

d1.writeFloat(22.3f);

d1.writeBoolean(false);

d1.writeUTF(“aaaa”);//Unicode Transformation Format (UTF)

System.out.println(“data written in file”);

 

}

 

}

 

Ruls:

  • According to Java Course In Pune, to read data as primitive type using redxx() method ,first of all the data must be wriitten to the file as primitive type using writexxx() methods. 
  • readxxmethod must be called in the same order in which the writexx() method are called  otherwise wrong value will be return or EOF exception is raised.
  • readxx() method read number of bytes from file based on the data type. for ex if we call readInt() methd it read 4 byte from file .so if four btes are available in file programm executed fine ,else programm execution terminate.

DataInputStream

 

Java DataInputStream class allows an application to read primitive data from the input stream in a machine-independent way.

 

According to Java Classes In Pune, Java application generally uses the data output stream to write data that can later be read by a data input stream.

public class DataInputStream extends FilterInputStream implements DataInput  

 

import java.io.DataInputStream;

import java.io.FileInputStream;

import java.io.IOException;

 

public class DatainputStremm {

 

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

FileInputStream f=new FileInputStream(“F:\\java\\myfile.txt);

// DataInputStream d11=new DataInputStream(f);

 

//int x=d11.readInt();

//System.out.println(x);

  DataInputStream inst = new DataInputStream(f);  

    int count = f.available();  

    byte[] ary = new byte[count];  

    inst.read(ary);  

    for (byte bt : ary) {  

      char k = (char) bt;  

      System.out.print(k+”  “);  

}

}

}

 

Author:-
Ghodekar, Pooja

Call the Trainer and Book your free demo Class for JAVA now!!!

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*