I am new to databases in Java and i am trying to export the data from 1 table and store it in a text file. At the moment the code below writes to the text file however all on one line? can anyone help?

**My Code** 

```
    private static String listHeader() {
            String output = "Id Priority From                           Label  Subject\n";
            output += "== ======== ====                           =====  =======\n";
            return output;
        }
    public static String Export_Message_Emails() {
            String output = listHeader();
            output +="\n";
            try {
                ResultSet res = stmt.executeQuery("SELECT * from messages ORDER BY ID ASC");
                while (res.next()) { // there is a result
                    output += formatListEntry(res);
                    output +="\n";

                }
            } catch (Exception e) {
                System.out.println(e);
                return null;
            }
            return output;
        }
public void exportCode(String File1){
     try {
                if ("Messages".equals(nameOfFile)){
                 fw = new FileWriter(f);   
                //what needs to be written here
                //fw.write(MessageData.listAll());
                fw.write(MessageData.Export_Message_Emails());
                    fw.close(); 

          }
}

```