0

I want to create a file and use project name and date in the filename. Here is a simple Java code I used for a while on my Unix machine.

    String fileName = projectName + "," + (date.toString());
    PrintStream pr = new PrintStream(new File(fileName));

Recently, I tried to run my code on a Windows machine and I got this error:

Exception in thread "main" java.io.FileNotFoundException: Test,Thu Sep 03 12:28:33 EDT 2015 (The filename, directory name, or volume label syntax is incorrect) 

When I remove the date from the filename, everything works perfect. Any suggestion what is the problem of date in filename and how I can fix it?

MTT
  • 5,113
  • 7
  • 35
  • 61
  • Print out the file name once you've concatenated the date. Is there a file with that String as its name? – CubeJockey Sep 03 '15 at 16:40
  • 1
    Probably the file name contains character that are not allowed on Windows like `:`. – Titus Sep 03 '15 at 16:41
  • date likely contains / or : or some other illegal character http://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names – Jeff Miller Sep 03 '15 at 16:42
  • 2
    Use a date format like `yyyy-MM-dd-HH-mm-ss` to avoid file naming problems. – RealSkeptic Sep 03 '15 at 16:43

3 Answers3

6

The name Test,Thu Sep 03 12:28:33 EDT 2015 is invalid on Windows because it contains a :. : is reserved for delimiting the end of a drive letter (e.g., c:). You can't have colons in filenames on Windows.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • That's half the answer ("what is the problem"). The other half is "how I can fix it". :-) – Andreas Sep 03 '15 at 16:49
  • @Andreas: I should have thought that was obvious: Don't put `:` in your filename. *How* you do that is an incredibly broad topic. – T.J. Crowder Sep 03 '15 at 16:50
  • Sure, but a suggestion, like RealSkeptic's comment to the question, would make your answer *complete*. – Andreas Sep 03 '15 at 16:52
3

There are a few limitations for characters which are allowed for filenames in windows.

See the official Windows file naming limitations at MSDN

I would suggest specifying a format when calling date.toString().

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Manuel Zametter
  • 309
  • 1
  • 8
0

Instead of using date.toString() which can be different depending on the OS and it may turns out in invalid characters for file name. Try to use d.getTime() so your code will be:

String fileName = projectName + "-" + date.getTime();

getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object.

And also change the comma. As it can be problematic as well.

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • You can still use date.toString(), but specify an appropriate format string. – Manuel Zametter Apr 04 '17 at 10:02
  • @ManuelZametter This suggestion avoids the OP the need to know which characters are invalid in the OS, therefore no need for a format which may have a format with dashs or something valid. The idea here is to not spend useless lines of code. K.I.S.S. always. – Jorge Campos Apr 04 '17 at 11:37