mardi 29 septembre 2015

Write in text file with line break java [duplicate]

This question already has an answer here:

I have created a simple text file to store certain events (More like a log file but a simpler approach) The text file keeps track on each download attempt by users.

The problem that whenever I have more than an attempt in the same session per user, the text file will be reset and will only store the last download attempt.

Desired output:

username: user1 File_Name: myfile.zip Downloaded_Time: yyyy-mm-dd HH:mm:ss
username: user2 File_Name: myfile.zip Downloaded_Time: yyyy-mm-dd HH:mm:ss
username: user3 File_Name: myfile.zip Downloaded_Time: yyyy-mm-dd HH:mm:ss

Actual output:

username: user3 File_Name: myfile.zip Downloaded_Time: yyyy-mm-dd HH:mm:ss

Here is my work so far:

try
                {
                    String content = "Username = "+ uname + " File_Name: "+filename+" Downloaded_Time: " +myDate;
                    File file = new File("C:\\log\\log.txt");

                    if(!file.exists())
                    {
                        file.createNewFile();
                    }

                    PrintWriter pw =new PrintWriter(file.getPath());
                    pw.println(content);
                    pw.print("\n");
                    pw.close();


                }catch (IOException e)
                {
                    e.printStackTrace();
                }

Aucun commentaire:

Enregistrer un commentaire