Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Colon in FileName

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Drew
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not on Windows.
 
Greg Pfeil
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add escape character before the colon like 04\:15\:44.txt. It will work for Mac OS also
Thanks
V.S.Saravanan
 
timothy zimmerman
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think that will even compile ... will it?
I tried and got illegal escape character compilation errors
FileNameTest.java [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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic