-2

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.

        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);
            }
       }

Unfortunately when I run the above code I get the following error:

enter image description here

I cannot find the problem, please tell me what I am doing wrong.

  • you cant have slashes (`/`) in your filename – lordvlad Dec 17 '16 at 14:12
  • I tried with `new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");` but I am still getting the same error. – Moiz Mansoor Ali Dec 17 '16 at 14:15
  • @lordvlad I tried it now and it worked. The format `yyyy-MM-dd HH-mm-ss` works. Thanks for the Help. – Moiz Mansoor Ali Dec 17 '16 at 14:18
  • 1
    Seriously? I can't see how an error message could be more clear than that? It almost literally tells you that you cannot name your file like this! – Fildor Dec 17 '16 at 14:19
  • @Fildor Yes I realize that now that the answer is pretty simple, I am still new to programming and stack overflow. Please tell me If you think that this question should be removed because it will be of no use to others in the future. – Moiz Mansoor Ali Dec 17 '16 at 14:42
  • That's not up to me to decide. It earns down votes so I would delete it. But it is your decision. – Fildor Dec 17 '16 at 17:33

2 Answers2

1

Guessing: either

  1. your operating system doesn't allow to use the / character in file names
  2. or it thinks that / separates directories; in other words: you are trying to create a file in a subdirectory ... that probably doesn't exist

And unrelated, but important too: you should not mix such things. You should put the code that creates and writes that file into its own utility class; instead of pushing it into your UI related code.

You see, if you had created a helper class here; it would also be much easier to do some unit-testing on that one; to ensure it does what you expect it to do.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Thank you for telling me this, I changed the date format to `yyyy-MM-dd HH-mm-ss` and the program works. – Moiz Mansoor Ali Dec 17 '16 at 14:21
  • @MoizMansoorAli Glad to hear that. If so, please consider accepting my answer then (still possible, even for closed questions). – GhostCat Dec 17 '16 at 20:06
0

Filesystems have limitations on what characters can go into file names. For example, as @lordvlad says, slashes are used to divide between success directories. Also, in Windows, the : is used to separate the drive name (i.e. C:\...).

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Yeah I completely forgot that the compiler could misinterpret the `/` and the `:` symbols as a file path instead of the file name. Thanks for the help. – Moiz Mansoor Ali Dec 17 '16 at 14:25
  • 1
    It is not the compiler. It is the file system. All of which have rules what filenames and paths are allowed to look like. – Fildor Dec 17 '16 at 14:30