Creating a text file with the current date and time as the file name in Java : <p>I am trying to create a text file and add some details into it using Java when a button is clicked in my GUI application, the name of the text file has to be the current date and time and the location of the text file has to be relative. Here is the code snippet I used to do this.</p>

<pre><code>        public void actionPerformed(ActionEvent e){
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd_HH:mm:ss");
            Date date = new Date();
            String fileName = dateFormat.format(date) + ".txt";
            File file = new File(fileName);
            PrintWriter pw;
            try{
                if(file.createNewFile()){
                    pw = new PrintWriter(file);

                    //Write Details To Created Text File Here

                    JOptionPane.showMessageDialog(null, "The Statistics have successfully been saved to the file: "
                            + fileName);
                }else{
                    JOptionPane.showMessageDialog(null, "The save file " + fileName
                            + " already exists, please try again in a while.");
                }
            }catch(IOException exception){
                JOptionPane.showMessageDialog(null, exception + ", file name:- " + fileName);
            }catch(Exception exception){
                JOptionPane.showMessageDialog(null, exception);
            }
       }
</code></pre>

<p>Unfortunately when I run the above code I get the following error:</p>

<p><a href="https://i.stack.imgur.com/F49SV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F49SV.png" alt="enter image description here"></a></p>

<p>I cannot find the problem, please tell me what I am doing wrong.</p>
