I’m trying to write multiple lines into a file but only one record of EmpID, DeptID and Salary is getting stored. Below is the code snippet. How do I write multiple lines?
case 1:try
{
FileWriter fsalary_specific = new FileWriter(
new File("Salary_Specific.txt"));
DeptID = tokens[2];
String var_2 = tokens[3];
salary = Double.parseDouble(var_2);
fsalary_specific.write(EmpID+" "+DeptID+" "+salary+"\n");
fsalary_specific.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
You need to open the file in “append” mode if you are re-opening it every time. Pass
trueinto theFileWriterconstructor like so: