Q:

Making a new line in a file

Im trying to write a text to file, but it overwrites whats inside, can somebody explain how to check if the text exists and then put a new line and write? Here is the code I am working with:
try
{
    if (!file.exists())
    {
        file.createNewFile();
    }

    FileWriter fw =
        new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.newLine();
    bw.write(petName);
    bw.close();
    System.out.println("Done!");
}
catch (IOException exc)
{
    System.out.println(exc);
}

Thank you for any kind of help.

A:

how to check if the text exists and then put a new line and write?

Use File#length() to check if the text exists in the file.
if(file.length()>0){
    bw.newLine();
}

Note: 
Open the file in append mode if you don't want to override the existing content of the file.

