Santosh  V M
Santosh V M

Reputation: 1571

Trying to write multiple lines to a file but file has only 1 line (or the final values)

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;

Upvotes: 0

Views: 3213

Answers (2)

gnomed
gnomed

Reputation: 5565

You need to open the file in "append" mode if you are re-opening it every time. Pass true into the FileWriter constructor like so:

 FileWriter fsalary_specific = new FileWriter(new File("Salary_Specific.txt"), true);

Upvotes: 4

Andrew Thompson
Andrew Thompson

Reputation: 168845

See FileWriter(File,boolean). If that does not work for you, post an SSCCE.

Upvotes: 2

Related Questions