Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

---File Handling Concept ---

What is a File?



the file is a collection of records, it is used to store program input data and output data permanently under System hard-disk(ROM).

 Java Provide java.io package to handle file operation.  it contains predefined classes and methods to store records.

 Type of file:-

1.1 Binary file:-  It will read and write data in the byte pattern.


1.2 Text file:-   It will read and Write data in char pattern.



                                                 java.io   (package)


                                                            File     


(Text File )                                                           (Binary File)

FileWriter                                                           FileOutputStream
BufferedWriter                                                  FileInputStream
FileReader
BufferedReader

 .........................................................................................................................................

Q) Create File Handling Program to Write data on file?

package scs;
import java.io.*;
public class FileHandlingProgram {

public static void main(String[] args) throws IOException{
File f = new File("d://hello.txt");
FileWriter fw = new FileWriter(f);
fw.write("Hello World");
fw.close();

}

}
 

Q) Create a program to read data on file?

package scs;
import java.io.*;
public class FileHandlingProgram {

public static void main(String[] args) throws IOException{
File f = new File("d://hello.txt");
FileReader fw = new FileReader(f);
    BufferedReader br = new BufferedReader(fw);
    String s ="";
    String s1="";
    while((s1 = br.readLine())!=null)
    {
    s = s+s1+"\n";
    }
    System.out.print(s);
fw.close();

}

}


Q) File handling program to create, read, write and append data using InputStreamReader class?

import java.io.*;
class Filehandling
{
     File f;
     void createFile() throws IOException
    {
      f = new File("d://fileexample.txt");
      if(!f.exists())
      f.createNewFile();

    }

    void writeFile() throws IOException
    {
      FileWriter fw = new FileWriter(f);
      BufferedWriter bw = new BufferedWriter(fw);
      System.out.println("Enter data");
      InputStreamReader ir = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(ir);
      String s = br.readLine(); 
      bw.write(s);
      bw.close();
      fw.close();
               

    }
   
   void readFile() throws IOException
   {
       FileReader fr = new FileReader(f);
       BufferedReader br = new BufferedReader(fr);
       String s="",s1="";
       while((s = br.readLine())!=null)
       {
           s1 = s1+s;
       }
       System.out.println(s1);
   }
   void appendFile() throws IOException
   {
        FileWriter fw = new FileWriter(f,true);
        BufferedWriter bw = new BufferedWriter(fw);
        System.out.println("Enter data");
      InputStreamReader ir = new InputStreamReader(System.in);
      BufferedReader br = new BufferedReader(ir);
      String s = br.readLine(); 
      bw.write(s);
      bw.close();
      fw.close();
   }

}

class FileMain
{
    public static void main(String args[]) throws IOException
    {
        Filehandling obj = new Filehandling();
        obj.createFile();
        obj.appendFile();
        obj.readFile();

    }

}


Q) Create a Program to Create, Write, Read, and Append Data in a text file?

 import java.io.*;
import java.util.Scanner;
class TextFileExample
{
     File f;
     Scanner sc = new Scanner(System.in);
     String name;
     String email;
     void createFile() throws IOException
     {
          f = new File("xyz.txt");
          if(!f.exists())
          f.createNewFile();
        
        
     }

    void writeFile() throws IOException
    {
       FileWriter fw = new FileWriter(f);
       BufferedWriter bw = new BufferedWriter(fw);
       bw.write("welcome in file handling ");
       System.out.println("enter name and email");
       name=sc.nextLine();
       email= sc.next();
       bw.write("name is "+name+" email is "+email);
       bw.close();
       fw.close();

    }

    void readFile() throws IOException
    {
      FileReader fr = new FileReader(f);
      BufferedReader br = new BufferedReader(fr);
      String s="",s1="";
      while((s=br.readLine())!=null)
      {
         s1+=s;
      }
      System.out.println(s1);
      br.close();  
      fr.close();
    }

    void appendFile() throws IOException
    {
       FileWriter fw = new FileWriter(f,true);
       BufferedWriter bw = new BufferedWriter(fw);
       bw.write("welcome in file handling ");
       System.out.println("enter name and email");
       name=sc.nextLine();
       email= sc.next();
       bw.write("name is "+name+" email is "+email);
       bw.newLine();
       bw.close();
       fw.close();

    }

}

class FileMain
{
    public static void main(String args[]) throws IOException
    {
       TextFileExample obj = new TextFileExample();
       obj.createFile();
       //obj.writeFile();
       obj.appendFile();
       obj.readFile();
    }


}




What is the InputStreamReader Class?

It is used to take large input from users and manage input data under file.

Create Object Of InputStreamReader Class and pass System.in as a reference.

Create Object of  BufferedReader class and Pass InputStreamReader Object

use readLine() to take UserInput.


InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
String s = br.readLine();




Simple Example of InputStreamreader to create biodata?


package tuyht;
import java.io.*;
public class FileExampleDemo {

public static void main(String[] args) throws IOException {
File f = new File("C://Users//Hp//Desktop//about.txt");
System.out.println("Enter about you");
String s;
InputStreamReader ir = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(ir);
s = br.readLine();
FileWriter fw = new FileWriter(f,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.write(s);
bw.close();

}

}





Sample Program to Read and Write Data in File using InputStreamReader?


import java.io.*;

class TextFileExample
{
File f;
InputStreamReader ir = new InputStreamReader(System.in);
String name;
String email;
void createFile() throws IOException
{
f = new File("abc.txt");
if(!f.exists())
f.createNewFile();


}

void writeFile() throws IOException
{
BufferedReader sc = new BufferedReader(ir);
FileWriter fw = new FileWriter(f);
BufferedWriter bw = new BufferedWriter(fw);
System.out.println("Enter your details:");
System.out.println("name:");
name=sc.readLine();
System.out.println("Enter your email:");
email=sc.readLine();
System.out.println("Enter your phone no:");
String num=sc.readLine();

System.out.println("Enter your personal details:");
System.out.println("Enter your Date of birth in dd/mm/yyyy format:");
String dob=sc.readLine();
System.out.println("Enter your father's name:");
String fname= sc.readLine();
System.out.println("Enter your address:");
String add= sc.readLine();
System.out.println("Enter your hobbies:");
String hobby= sc.readLine();

System.out.println("Enter your educational qualifiacation");
System.out.println("Enter graduation year");
String grad= sc.readLine();

System.out.println("Enter the course name");
String course = sc.readLine();
System.out.println("Enter the Institute/University");
String Institute = sc.readLine();
System.out.println("Enter your experience");
String exp = sc.readLine();
bw.write(" Name : "+name);
bw.write("\n");
bw.write(" Email: "+email);
bw.write("\n");
bw.write(" Phone no: "+num);
bw.write("\n");
bw.write("\n");
bw.write(" PERSONAL DETAILS \n\n");
bw.write(" DOB: "+dob);
bw.write("\n");
bw.write(" Father's Name : "+fname);
bw.write("\n");
bw.write(" Address : "+add);
bw.write("\n");
bw.write(" Hobbies : "+hobby);
bw.write("\n");
bw.write("\n");
bw.write(" EDUCATIONAL QUALIFICATION \n\n");

bw.write("graduation year : "+grad);
bw.write("\n");
bw.write("Course : "+course);
bw.write("\n");
bw.write("Institute/University : "+Institute);
bw.write("\n");
bw.write("\n");
bw.write(" EXPERIENCE \n\n");
bw.write(exp);
bw.close();
fw.close();

}

void readFile() throws IOException
{
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String s="",s1="";
while((s=br.readLine())!=null)
{
s1+=s;
}
System.out.println(s1);
br.close();
fr.close();
}

void appendFile() throws IOException
{
FileWriter fw = new FileWriter(f,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.newLine();
bw.close();
fw.close();

}
}


class FileMain1
{
public static void main(String args[]) throws IOException
{
TextFileExample obj = new TextFileExample();
obj.createFile();
obj.writeFile();
// obj.appendFile();
obj.readFile();
}


}

.............................................................................................................................................................

Create Program to Read, Write, Append data using byte pattern for binary file?



Binary file:- 
if we write data using a binary pattern (0 and 1) then it is called binary file Java provides predefined classes that will be used to handle binary file operation.


FileOutputStream:-  It is used to write data on file using the binary form.


FileInputStream:-     It is used to read data on file using the binary form.


Java provides getByte() to convert char data to byte pattern


String s = "hello";

byte arr[] = s.getByte();


A simple program to write data on file?

package tuyht;
import java.io.*;
public class FileExampleDemo {

public static void main(String[] args) throws IOException {
File f = new File("C://Users//Hp//Desktop//about.txt");
String s = "hello";
FileOutputStream fw = new FileOutputStream(f);
fw.write(s.getBytes());
fw.close();

}

}



Sample Program to Read File Data from binary:-


package tuyht;
import java.io.*;
public class FileExampleDemo {

public static void main(String[] args) throws IOException {
File f = new File("C://Users//Hp//Desktop//about.txt");
String s = "hello";
FileInputStream fw = new FileInputStream(f);
byte arr[] = new byte[60];
int a=0;
while((a=fw.read(arr))!=-1)
{
String s1 = new String(arr);
System.out.print(s1);
}
fw.close();

}

}



Complete Program of Binary Input, Output Operation:-

import java.io.*;
class BinaryFile
{
    File f;
    void createFile() throws IOException
    {
        f= new File("xyz.txt");
        if(!f.exists())
        {
           f.createNewFile();
        }


    }
    void writeFile() throws IOException
    {
        FileOutputStream fo = new FileOutputStream(f);
        String s ="hi"; 
        byte arr[] = s.getBytes();  //string to byte
        fo.write(arr);
        fo.close();
    }
    void readFile() throws IOException
    {
       FileInputStream fi = new FileInputStream(f);
       int data;
       byte arr[] = new byte[10];
       while((data=fi.read(arr))!=-1)
       {
            String s = new String(arr);  //byte to string
            System.out.println(s);
       } 
      fi.close();

    }
     void appendFile() throws IOException
    {
        FileOutputStream fo = new FileOutputStream(f,true);
        String s ="hello"; 
        byte arr[] = s.getBytes();  //string to byte
        fo.write(arr);
        fo.close();
    }

public static void main(String args[]) throws IOException
{
    BinaryFile obj = new BinaryFile();
    obj.createFile();
   // obj.writeFile();
    obj.appendFile();
    obj.readFile();
}



ASSIGNMENT:

CREATE RESUME WHERE OBJECTIVE, QUALIFICATION, PROJECT WORK, FULL NAME, MOBILE NO will be enereted by the user?

WAP to create marksheet using binary file pattern and display result of multiple students under a single file.





1 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. Program of file handling:

    There is no error but not taking input for father's name and course name .

    import java.io.*;
    import java.util.Scanner;
    class TextFileExample
    {
    File f;
    Scanner sc = new Scanner(System.in);
    String name;
    String email;
    void createFile() throws IOException
    {
    f = new File("abc.txt");
    if(!f.exists())
    f.createNewFile();


    }

    void writeFile() throws IOException
    {
    FileWriter fw = new FileWriter(f);
    BufferedWriter bw = new BufferedWriter(fw);
    System.out.println("Enter your details:");
    System.out.println("name:");
    name=sc.nextLine();
    System.out.println("Enter your email:");
    email= sc.next();
    System.out.println("Enter your phone no:");
    int num=sc.nextInt();

    System.out.println("Enter your personal details:");
    System.out.println("Enter your Date of birth in dd/mm/yyyy format:");
    String dob=sc.next();
    System.out.println("Enter your father's name:");
    String fname= sc.nextLine();
    System.out.println("Enter your address:");
    String add= sc.nextLine();
    System.out.println("Enter your hobbies:");
    String hobby= sc.nextLine();

    System.out.println("Enter your educational qualifiacation");
    System.out.println("Enter graduation year");
    int grad= sc.nextInt();
    /*int temp=grad;
    int count=0;
    do{
    grad=grad/10;
    ++count;
    }while(grad!=0);
    if(count!=4)
    {
    System.out.println("reenter year");
    int year=sc.nextInt();
    System.out.println(t);
    }*/ // incomplete
    System.out.println("Enter the course name");
    String course = sc.nextLine();
    System.out.println("Enter the Institute/University");
    String Institute = sc.nextLine();
    System.out.println("Enter your experience");
    String exp = sc.nextLine();
    bw.write(" Name : "+name);
    bw.write("\n");
    bw.write(" Email: "+email);
    bw.write("\n");
    bw.write(" Phone no: "+num);
    bw.write("\n");
    bw.write("\n");
    bw.write(" PERSONAL DETAILS \n\n");
    bw.write(" DOB: "+dob);
    bw.write("\n");
    bw.write(" Father's Name : "+fname);
    bw.write("\n");
    bw.write(" Address : "+add);
    bw.write("\n");
    bw.write(" Hobbies : "+hobby);
    bw.write("\n");
    bw.write("\n");
    bw.write(" EDUCATIONAL QUALIFICATION \n\n");

    bw.write("graduation year : "+grad);
    bw.write("\n");
    bw.write("Course : "+course);
    bw.write("\n");
    bw.write("Institute/University : "+Institute);
    bw.write("\n");
    bw.write("\n");
    bw.write(" EXPERIENCE \n\n");
    bw.write(exp);
    bw.close();
    fw.close();

    }

    void readFile() throws IOException
    {
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
    String s="",s1="";
    while((s=br.readLine())!=null)
    {
    s1+=s;
    }
    System.out.println(s1);
    br.close();
    fr.close();
    }

    void appendFile() throws IOException
    {
    FileWriter fw = new FileWriter(f,true);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.newLine();
    bw.close();
    fw.close();

    }
    }


    class FileMain
    {
    public static void main(String args[]) throws IOException
    {
    TextFileExample obj = new TextFileExample();
    obj.createFile();
    obj.writeFile();
    // obj.appendFile();
    obj.readFile();
    }


    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post