Skip to main content

Binary File and Text File in Java Notes

 Binary File

For Creating/Storing Information into the file


FileOutputStream fout = new FileOutputStream(new File("filename",true)); //true for append

DataOutputStream ffout = new DataOutputStream(fout);


Functions to store data:


Integers - ffout.writeInt();

Floating - ffout.writeFloat();

Double - ffout.writeDouble();

char - ffout.writeChar();

String - ffout.writeUTF();



For reading the file 


FileInputStream fin = new FileInputStream(new File("filename"));

DataInputStream ffin = new DataInputStream(fin);


Functions to read data:

Integers - ffin.readInt();

Floating - ffin.readFloat();

Double - ffin.readDouble();

char - ffin.writeChar();

String - ffin.writeUTF();


Example: WAP a program to store 10 names and in a binary file and read the data


import java.util.*;

import java.io.*;

class demo

{

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

 {

  FileOutputStream fout = new FileOutputStream(new File("STUDENT.DAT"),true); //true for append

  DataOutputStream ffout = new DataOutputStream(fout);

  Scanner sc = new Scanner(System.in);

  for(int i=0;i<10;i++)

  {

   System.out.println("Enter the name: ");

   String name = sc.next();

   System.out.println("Enter roll: ");

   int roll = sc.nextInt();

   ffout.writeUTF(name);

   ffout.writeInt(roll);

  }

  ffout.close();

  fout.close();

  FileInputStream fin = new FileInputStream(new File("STUDENT.DAT"));

  DataInputStream ffin = new DataInputStream(fin);

  boolean eof=false;

  try

  {

   while(!eof)

   {

    String name = ffin.readUTF();

    int roll = ffin.readInt();

    System.out.println("Name: "+name);

    System.out.println("Roll: "+roll);

   }

  }catch(Exception e)

  {

   eof=true;

   System.out.println("File reading complete");

  }

  ffin.close();

  fin.close();

  sc.close();

 }

}


Text File
---------------
For creating/storing data into file
------------------------------------


FileWriter fw = new FileWriter("filename",true); //append 

BufferedWriter bw = new BufferedWriter(fw);

PrintWriter pw = new PrintWriter(bw);


Function to data into file

--------------------------

pw.println()



For reading data

-----------------


Scanner read = new Scanner(new File("filename"));


while(read.hasNext())

{

String name = read.next();

int roll = read.nextInt();

}


Example: WAP to accept 5 names and roll in a text file. Read the text and display the items.


import java.util.*;

import java.io.*;

class demo

{

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

 {

  Scanner sc = new Scanner(System.in);

  FileWriter fw = new FileWriter("students.txt",true); //append 

  BufferedWriter bw = new BufferedWriter(fw);

  PrintWriter pw = new PrintWriter(bw);

  String name;

  int roll;

  for(int i=0;i<2;i++)

  {

   System.out.println("Enter name: ");

   name = sc.next();

   System.out.println("Enter roll: ");

   roll = sc.nextInt();

   pw.println(name);

   pw.println(roll);

  }

  pw.close();

  bw.close();

  fw.close();

  sc.close();

  Scanner read = new Scanner(new File("students.txt"));

  System.out.println("Name\t\tRoll");

  while(read.hasNext())

  {

   name = read.next();

   roll = read.nextInt();

   System.out.println(name+"\t\t"+roll);

  }

  read.close();

 }

}

Popular posts from this blog