File Handling Concept in Java

16

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) Another Example to Write data on file?
import java.io.*;
import java.util.Scanner;
class FileExample
{
    public static void main(String args[]) throws IOException
    {
         Scanner sc = new Scanner(System.in);
         String s;
         System.out.println("Enter file content");
         s = sc.nextLine();
         File f = new File("shiva.txt");
         if(!f.exists())
         {
          f.createNewFile();    
         }
         FileWriter fw = new FileWriter(f,true);
         BufferedWriter bw = new BufferedWriter(fw);
         bw.newLine();
         bw.write(s); 
         bw.close();
         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();
}
}
Another Example to Read Data on File:-
import java.io.*;
import java.util.Scanner;
class FileExample
{
    public static void main(String args[]) throws IOException
    {
         Scanner sc = new Scanner(System.in);
         String s="",v="";
         File f = new File("shiva.txt");
         FileReader fr = new FileReader(f);
         BufferedReader br = new BufferedReader(fr);
         while((s = br.readLine())!=null)
         {
            v  = v+s;
         }
         System.out.println(v);
          br.close();
         fr.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.
Tags

Post a Comment

16Comments

POST Answer of Questions and ASK to Doubt

  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
  2. WAP to merge two files in one file.
    import java.io.*;
    class Main {
    public static void main(String[] args)throws IOException {

    String str="Yogita ";
    String str2="Patidar";

    File f=new File("d://name.txt");
    File f2=new File("d://surname.txt");
    File f3=new File("d://fullname.txt");

    FileWriter fw=new FileWriter(f);
    FileWriter fw2=new FileWriter(f2);
    FileWriter fwm=new FileWriter(f3,true);

    BufferedWriter bw=new BufferedWriter(fw);
    BufferedWriter bw2=new BufferedWriter(fw2);
    BufferedWriter bwm=new BufferedWriter(fwm);

    FileReader fr=new FileReader(f);
    FileReader fr2=new FileReader(f2);
    FileReader frm=new FileReader(f3);

    BufferedReader br=new BufferedReader(fr);
    BufferedReader br2=new BufferedReader(fr2);
    BufferedReader brm=new BufferedReader(frm);

    bw.write(str);
    bw2.write(str2);

    bw.close();
    bw2.close();

    while((str=br.readLine())!=null) {
    bwm.write(str);;
    }
    while((str=br2.readLine())!=null) {
    bwm.write(str);
    }

    bwm.close();


    }
    }

    ReplyDelete
  3. // WAP to count files present in a specific directory
    import java.io.*;
    class Main{
    static int count =0;
    public static void main(String[] args) {
    File file=new File("F:");
    countFiles(file);
    System.out.print(count);
    }

    public static void countFiles(File dirPath) {
    File fileList[]=dirPath.listFiles();
    try {
    for(File f: fileList) {
    if(f.isFile())
    count++;
    else
    countFiles(f);
    }
    }
    catch(NullPointerException e) {

    }
    }

    }

    ReplyDelete
  4. // Marksheet
    import java.io.*;
    import java.util.Scanner;

    class Marksheet{
    private int [] sub=new int[5];
    private String name="";
    private String[] subject= {"English","Hindi","Maths","Physics","Chemistry"};
    private int total=0;

    void accept()throws Exception {
    InputStreamReader r=new InputStreamReader(System.in);
    BufferedReader br=new BufferedReader(r);
    System.out.println("Enter student details");
    System.out.print("Enter name: ");
    name=br.readLine();
    System.out.print("Enter Marks: ");
    for(int i=0;i<5;i++) {
    System.out.print(subject[i]+":\t");
    sub[i]=Integer.parseInt(br.readLine());

    if(sub[i]>100||sub[i]<0) {
    sub[i]=Integer.parseInt(br.readLine());
    }
    if(sub[i]>100||sub[i]<0) {
    sub[i]=Integer.parseInt(br.readLine());
    }
    total=total+sub[i];
    }

    }

    void result()throws IOException {
    FileWriter fw=new FileWriter("f://Marksheet.txt",true);
    BufferedWriter bw=new BufferedWriter(fw);
    bw.write("\n\tMarksheet\n");
    bw.write("Name: "+name);
    bw.write("\nMarks:\n");
    for(int i=0;i<5;i++) {
    bw.write(subject[i]+" "+sub[i]+"\n");
    }
    bw.write("Percentage: "+(total/5));
    bw.newLine();
    bw.close();
    }

    void display()throws IOException{
    FileReader fr=new FileReader("f://Marksheet.txt",true);
    BufferedReader br=new BufferedReader(fr);
    String s="";
    while((s=br.readLine())!=null) {
    System.out.print(s);
    }
    }
    }

    public class Frame1{

    public static void main(String[] args)throws IOException {
    try {
    int i,num=0;
    Scanner scan=new Scanner(System.in);

    System.out.print("Enter number of students : ");
    num=scan.nextInt();
    Marksheet r[]=new Marksheet[num];

    for(i=0;i<r.length;i++) {
    r[i]=new Marksheet();
    r[i].accept();
    }
    for(i=0;i<r.length;i++) {
    r[i].result();
    if(i==(num-1))
    r[i].display();
    }

    }
    catch(Exception e) {
    System.out.print(e);
    }
    }

    }

    ReplyDelete
  5. import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    public class FileHandlingExample1 {
    File f;
    void createFile() throws IOException
    {
    f = new File("resume1.doc");
    if(!f.exists())
    {
    f.createNewFile();
    }
    }
    void writeFile() throws IOException
    {
    FileWriter fw = new FileWriter(f);
    BufferedWriter bw = new BufferedWriter(fw);
    String sname="xyz",email="abcd@gmail.com";
    bw.write("name is ="+sname);
    bw.newLine();
    bw.write("emailid is ="+email);
    bw.close();
    }
    void readFile() throws IOException
    {
    FileReader fr = new FileReader("d://hello.txt");
    BufferedReader br = new BufferedReader(fr);
    String s="";
    String data="";
    while(( data = br.readLine())!=null)
    {
    s = s+data;
    }
    System.out.print(s);
    }
    void appendFile() throws IOException
    {
    FileWriter fw = new FileWriter(f,true);
    BufferedWriter bw = new BufferedWriter(fw);
    String sname="xyz",email="abcd@gmail.com";
    bw.write("name is ="+sname);
    bw.newLine();
    bw.write("emailid is ="+email);
    bw.close();
    }
    public static void main(String[] args) throws IOException {

    FileHandlingExample1 obj = new FileHandlingExample1();
    obj.createFile();
    obj.writeFile();
    obj.readFile();

    }

    }

    ReplyDelete
  6. #File Handling Example
    import java.io.*;
    class FileExample
    {
    public static void main(String args[]) throws IOException
    {
    File f = new File("shiva.txt");
    if(!f.exists())
    {
    f.createNewFile();
    }
    FileWriter fw = new FileWriter(f,true);
    fw.write("om namah shivay");
    fw.close();

    }

    }

    ReplyDelete
  7. //yogesh seroke
    //FileHandling

    import java.io.*;
    class FileHandling
    {
    public static void main(String args[])throws IOException
    {
    File f=new File("c://yogeshseroke//File.txt");
    if(!f.exists())
    {
    f.createNewFile();
    }
    FileWriter fw=new FileWriter(f);
    fw.writer("yogesh seroke");
    fw.close();
    }
    }

    ReplyDelete
  8. //yogesh seroke
    //FileHandling

    import java.io.*;
    class FileHandling1
    {
    public static void main(String args[])throws IOException
    {
    File f=new File("c://yogeshseroke//File.txt");
    if(!f.exists())
    {
    f. createNewFile();
    }
    FileWriter fw=new FileWriter(f,true);
    fw.write(" how are you...?");
    fw.close();
    }
    }

    ReplyDelete
  9. //yogesh seroke
    //resume using filehandling

    import java.io.*;
    class FileHandling2
    {
    public static void main(String args[])throws IOException
    {
    File f=new File("c://yogeshseroke//File1.txt");
    if(!f.exists())
    {
    f.createNewFile();
    }
    FileWriter fw=new FileWriter(f);
    fw.write("RESUME");
    fw.close();

    FileWriter fw1=new FileWriter(f,true);
    fw1.write(" \n name \t\t : \t yogesh seroke");
    fw1.close();

    FileWriter fw2=new FileWriter(f,true);
    fw2.write(" \n mobile number \t : \t 143");
    fw2.close();

    FileWriter fw3=new FileWriter(f,true);
    fw3.write(" \n email I'd \t\t : \t abc123@gmail.com");
    fw3.close();

    FileWriter fw4=new FileWriter(f,true);
    fw4.write(" \n skills \t\t : \t Java,Web Designning");
    fw4.close();

    FileWriter fw5=new FileWriter(f,true);
    fw5.write(" \n graduation \t : \t B.E. from IET DAVV(indore, mp) ");
    fw5.close();

    FileWriter fw6=new FileWriter(f,true);
    fw6.write(" \n school \t : \t Excillance Bal Vinay Mandir(indore,mp");
    fw6.close();

    }
    }

    ReplyDelete
  10. //yogesh seroke
    //binary example...
    import java.io.*;
    class Byte1
    {
    public static void main(String args[])throws IOException
    {
    File f=new File("c://yogeshserokefilehandling//byte.txt");

    FileInputStream fi=new FileInputStream(f);

    Byte ar[]=new Byte[10];

    int a=0;

    while((a=fi.read(ar))!=-1)
    {
    String s=new String(ar);
    System.out.println(s);
    }
    fi.close();
    }
    }

    ReplyDelete
  11. //yogesh seroke
    //binary file example....

    import java.io.*;
    class Byte
    {
    public static void main(String args[])throws IOException
    {
    File f=new File("c://yogeshserokefilehandling//byte.txt");
    FileOutputStream fo=new FileOutputStream(f);
    String s="This is Binary example";
    fo.write(s.getBytes());
    fo.close();
    }
    }

    ReplyDelete
  12. //yogesh seroke
    //program to append string in byte form
    import java.io.*;
    class Byte2
    {
    public static void main(String args[]) throws IOException
    {
    File f=new File("c://yogeshserokefilehandling//byte.txt");

    FileOutputStream fo=new FileOutputStream(f,true);

    String s="\n this is second program of binary example to apend";

    fo.write(s.getBytes());

    fo.close();

    FileOutputStream fo1=new FileOutputStream(f,true);

    String s1="\n this is second String";

    fo1.write(s1.getBytes());

    fo1.close();

    FileOutputStream fo2=new FileOutputStream(f,true);

    String s2="\n this is third String";

    fo2.write(s2.getBytes());

    fo2.close();
    }
    }

    ReplyDelete
  13. //SANTOSH KUMAR PAL
    //Create File Handling Program to write data on File.
    import java.io.*;
    public class FileHandlingExample {

    public static void main(String[] args) throws IOException {
    File fi=new File("d://Santosh.text");
    FileWriter fw=new FileWriter(fi);
    fw.write("Java is easy to learn");
    fw.close();
    }

    }

    ReplyDelete
  14. //SANTOSH KUMAR PAL
    //Create File Handling Program to Write data on file using Scanner class?

    import java.io.*;
    import java.util.*;
    public class FileHandlingExample1 {

    public static void main(String[] args) throws IOException {
    Scanner sc=new Scanner(System.in);
    String s;
    System.out.println("Enter Name");
    s=sc.next();
    File fi=new File("d://Santosh.txt");
    if(!fi.exists())
    {
    fi.createNewFile();
    }

    FileWriter fw=new FileWriter(fi,true);
    BufferedWriter bw=new BufferedWriter(fw);
    bw.newLine();
    bw.write(s);
    bw.close();
    fw.close();
    }

    }

    ReplyDelete
  15. // Create a Program to Create, Write, Read, and Append Data in a text file?

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

    void writeFile() throws IOException
    {
    FileWriter fw = new FileWriter(fi);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("Yours welcome in file handling Program ");
    System.out.println("Enter name ");
    System.out.println("Enter email ");
    System.out.println("Enter Contact no ");
    name=sc.nextLine();
    email= sc.nextLine();
    cont=sc.next();
    bw.write("Name is "+name+" Email is "+email+"Contact is"+cont);
    bw.close();
    fw.close();

    }

    void readFile() throws IOException
    {
    FileReader fr = new FileReader(fi);
    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(fi,true);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("Yours welcome in file handling Program");
    System.out.println("Enter name");
    System.out.println("Enter email");
    System.out.println("Enter contact");
    name=sc.nextLine();
    email= sc.nextLine();
    cont=sc.next();
    bw.write("Name is "+name+" Email is "+email+"Contact no is"+cont);
    bw.newLine();
    bw.close();
    fw.close();

    }

    }

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


    }


    ReplyDelete
  16. //Sample Program to Read and Write Data in File using InputStreamReader?

    import java.io.*;
    import java.util.*;
    public class TextFilehandling1 {
    File fi;
    InputStreamReader ir=new InputStreamReader(System.in);
    String name;
    String email;
    String cont;
    void createFile() throws IOException
    {
    fi=new File("d://BIODATA.txt");
    if(!fi.exists())
    fi.createNewFile();
    }
    void writeFile() throws IOException
    {
    BufferedReader sc=new BufferedReader(ir);
    FileWriter fw=new FileWriter(fi);
    BufferedWriter bw=new BufferedWriter(fw);
    System.out.println("Enter Your Detail");
    System.out.println("Enter Name");
    name=sc.readLine();
    System.out.println("Enter Email");
    email=sc.readLine();
    System.out.println("Enter Contact no");
    cont=sc.readLine();
    System.out.println("Enter Your Personal Detail");
    System.out.println("Enter Your DOB as dd/mm/yy");
    String dob=sc.readLine();
    System.out.println("Enter Your Father's Name");
    String fname=sc.readLine();
    System.out.println("Enter Your Mother's Name");
    String mname=sc.readLine();
    System.out.println("Enter Your Address");
    String addr=sc.readLine();
    System.out.println("Enter Your Hobbies");
    String hobby=sc.readLine();
    System.out.println("Enter Your Academic Qualification");
    String aqua=sc.readLine();
    System.out.println("Enter Your Post Graduation Year");
    String pgy=sc.readLine();
    System.out.println("Enter Your Extra Qualifications");
    String extqu=sc.readLine();
    System.out.println("Enter Your Course Name");
    String courn=sc.readLine();
    System.out.println("Enter Institute/university Name");
    String Inst=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("Contact:"+cont);
    bw.write("\n");
    bw.write("DOB:"+dob);
    bw.write("\n");
    bw.write("Father's Name:"+fname);
    bw.write("\n");
    bw.write("Mother's Name:"+mname);
    bw.write("\n");
    bw.write("Address:"+addr);
    bw.write("\n");
    bw.write("Hobbies:"+hobby);
    bw.write("\n");
    bw.write("Academic Qualification"+aqua);
    bw.write("\n");
    bw.write("P.G.Year:"+pgy);
    bw.write("\n");
    bw.write("Extra Qualification:"+extqu);
    bw.write("\n");
    bw.write("Course Name:"+courn);
    bw.write("\n");
    bw.write("Experience:"+exp);
    bw.write("\n");
    bw.write("Institute:"+Inst);
    bw.close();
    fw.close();
    }
    void readFile() throws IOException
    {
    FileReader fr=new FileReader(fi);
    BufferedReader br=new BufferedReader(fr);
    String s="",s1="";
    while((s=br.readLine())!=null)
    {
    s1=s+s1;
    }
    System.out.println(s1);
    br.close();
    fr.close();
    }
    void appendFile() throws IOException
    {
    FileWriter fw=new FileWriter(fi);
    BufferedWriter bw=new BufferedWriter(fw);
    bw.newLine();
    bw.close();
    fw.close();
    }
    public static void main(String[] args) throws IOException {
    TextFilehandling1 obj=new TextFilehandling1();
    obj.createFile();
    obj.writeFile();
    obj.readFile();
    obj.appendFile();

    }

    }

    ReplyDelete
Post a Comment