• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Colon in FileName

 
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not on Windows.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic