javaeditplus

Record the current date to a .txt file in Java and saves it?


What's the simplest way to record the current date to a (text) file in Java? and that saves it so after time you will have a list of dates


Solution

  • Try this.

                var tdate =  new Date();
                File file = new File("filename.txt");
                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(tdate.toString());
                bw.close();