• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

File Handling program using Java

September 2, 2011 by ProjectsGeek Leave a Comment

File Handling program using Java
import java.io.*;
import java.util.*;
class record implements Serializable
{
static Scanner input=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int roll;
String name;

void insert()
{
try
{
System.out.println(“\n Enter Roll No:: “);
roll=input.nextInt();
System.out.println(“\n Enetr Name:: “);
name=br.readLine();
FileOutputStream fout=new FileOutputStream(“Student.txt”,true);
byte buff[]=name.getBytes();
fout.write((char)roll);
fout.write(‘\n’);
fout.write(buff);
fout.write(‘\n’);
fout.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void delete(int r)
{
try
{
FileInputStream fin = new FileInputStream(“student.txt”);
FileOutputStream fout=new FileOutputStream(“temp.txt”);

while(true)
{
int a=fin.read();
if(a==-1)
break;
if(a!=r)
{
fout.write((byte)a);
a=fin.read();
fout.write(a);
while(true)
{
a=fin.read();
if(a==’\n’)
break;
fout.write(a);
}
fout.write(a);
}
else
{
a=fin.read();
do{
a=fin.read();
}while(a!=’\n’);
}
}
fin.close();
fout.close();
File f=new File(“student.txt”);
File f1=new File(“temp.txt”);
if(f.delete())
f1.renameTo(new File(“student.txt”));
else
System.out.println(“Error”);

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void showdata(int r)
{
try
{
FileInputStream fin=new FileInputStream(“student.txt”);
while(true)
{

int a=fin.read();
if(a==-1)
break;
if(a==r)
{
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’\n’);
}
else
{
do
{
a=fin.read();
}while(a!=’\n’);
}

}

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}

void show()
{
try
{

FileInputStream fin=new FileInputStream(“student.txt”);
while(true)
{
int a=fin.read();
if(a==-1)
break;
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’\n’);

}

}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

public class Student extends record
{
public static void main(String args[])
{
try
{
while(true){
System.out.println(“\nEnter your choice : “);
System.out.println(“1:Insert.”);
System.out.println(“2:Delete.”);
System.out.println(“3:View record.”);
System.out.println(“4:View All.\n”);
int choice=input.nextInt();
record r=new record();
switch(choice)
{
case 1:
r.insert();
break;
case 2:
System.out.println(“Enter roll no. : “);
int roll=input.nextInt();
r.delete(roll);
break;
case 3:
System.out.println(“Enter roll no. : “);
roll=input.nextInt();
r.showdata(roll);
break;
case 4:
r.show();
break;
default:
System.out.println(“Invalid Entry !!”);
break;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Download Code

Other Projects to Try:

  1. Database connectivity in Java with MYSQL
  2. File Handling and IO Handling in Java Programming
  3. To Perform File Handling in Java
  4. Implement Conflation Algorithm using File Handling in Java
  5. Exception Handling In java

Filed Under: Java Assignments

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy