File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Colon in FileName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Colon in FileName" Watch "Colon in FileName" New topic

Colon in FileName

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I would like to put the time in my file name such as 04:15:44 but the colons seem to be causing a problem. I assume these are file seperators.
Is it possible to have a colon in the file name?
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Not on Windows.
Greg Pfeil

Joined: Apr 05, 2001
Posts: 11
Colons used to be file separators for Mac OS. The new OS X uses the slash as a file separator (like other Unices), but I think it may still allow the colon as a file separator, for Classic and Carbon stuff, I'm not sure.
So, yeah, it's a file separator. What are the legal characters in a filename in Java? I mean, Unix allows everything except slash. Java must be fairly strict. If they're trying to avoid all platform-specific file separators, that means no /, \, :, and > at least. I'm sure there are probably others, if we dig through enough obscure OSes.
Saravanan Subramanyan

Joined: Aug 08, 2002
Posts: 16
Add escape character before the colon like 04\:15\:44.txt. It will work for Mac OS also
timothy zimmerman
Ranch Hand

Joined: Jun 26, 2001
Posts: 149
I do not think that will even compile ... will it?
I tried and got illegal escape character compilation errors [11:1] illegal escape character
String fileName = "04\:15\:44.txt";
But ulitmately I think the answer is that java doesn't really care about the filename. It is the OS that the file is to be written on that will impose the limits.
So, if you know what OS you will be running on be sure not to use the file separator. You could use System.getProperty to get the file separator value and then check to make sure is is not in your filename.
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
In Windows, a file cannot contain special characters / \ : * ? | " < > ... there might be a few others.
In Unix, there are relatively fewer special characters that are not allowed in the file name, such as /. Different flavors of Unix probably have slightly different rules. For example, I remember working on an old variant of Unix called 'Xenix', and it was possible to create a file name with an embedded '?' character. I don't think you can do that on Solaris.
Checking for file names that contain file seperator (as returned by the path.seperator and file.seperator properties) is not sufficient. The OS may impose rules other than allowing specific characters. For example, you might not be allowed to create a file name with a leading space on some platforms. Another example -- some versions of Windows do not allow very long file names in very deep directories. The total length of the full pathname for the file must be less than some OS defined maximum. On Windows 2000, I tried to create a directory where the directory name was only 30 characters but the full path name was over 256 characters. I got 'The system could not find the path specified'.
So if the OS can't create a file with a particular name, neither can Java.
I agree. Here's the link:
subject: Colon in FileName
It's not a secret anymore!