package FileHandling;
public class Candidate {
private int experience;
private String name;
private String primarySkill;
private String qualification;
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrimarySkill() {
return primarySkill;
}
public void setPrimarySkill(String primarySkill) {
this.primarySkill = primarySkill;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String toString() {
return "Candidate [name=" + name + ", primarySkill=" + primarySkill+ ",experience=" + experience + ", qualification=" + qualification+ "]";
}
}
public class Candidate {
private int experience;
private String name;
private String primarySkill;
private String qualification;
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrimarySkill() {
return primarySkill;
}
public void setPrimarySkill(String primarySkill) {
this.primarySkill = primarySkill;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public String toString() {
return "Candidate [name=" + name + ", primarySkill=" + primarySkill+ ",experience=" + experience + ", qualification=" + qualification+ "]";
}
}
package FileHandling;
import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
public class ConsoleToObjectDemo1 {
public static void saveToFile(Candidate cand) throws IOException
{
String str=new String(cand.getName()+","+cand.getExperience()+","+cand.getPrimarySkill()+","+cand.getQualification());
File file=new File("abhishek.txt");
if(!file.exists())
file.createNewFile();
FileWriter fout=new FileWriter(file,true);
BufferedWriter bw=new BufferedWriter(fout);
bw.write(str);
bw.newLine();
bw.close();
fout.close();
System.out.println("Data is saved to file named "+file);
}
public static Candidate readCandidateDetails()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
Candidate c=new Candidate();
try
{
System.out.println("Enter name of the Candidate");
String name = br.readLine();
c.setName(name);
System.out.println("Enter Primary Skill");
String primarySkill = br.readLine();
c.setPrimarySkill(primarySkill);
System.out.println("Enter Qualification");
String qualification = br.readLine();
c.setQualification(qualification);
System.out.println("Enter Total Experience in Months");
int exp = Integer.parseInt(br.readLine());
c.setExperience(exp);
}catch(IOException ie)
{
System.out.println("IOException generated");
ie.printStackTrace();
}
return c;
}
public static void main(String[] args) throws IOException {
Candidate c=readCandidateDetails();
saveToFile(c);
}
}
0 comments:
Post a Comment