1

I am looking for a way to tell if a file represented by a file path can be successfully created. The scenario I am testing is a file path that is too long -- 300 characters, on Windows 7. What I have tried:

Files.get(filePath)

This does not throw.

file.getCanonicalFile()

This also does not throw.

!Files.exists(filePath) && !Files.notExists(filePath)

exists returns false and notExists returns true.

!file.canWrite()

canWrite returns true.

file.createNewFile()

This does not throw. "ls" indicates the file exists, however navigating to the containing directory in Windows Explorer shows the entire folder as empty. I had to use the command line to delete the bad file to get the folder contents to be displayed. This scenario is exactly the reason I want to validate the file path in the first place.

Any suggestions?

  • By your own admission the file was actually created, so there's no reason for the API to throw an exception. `ls` is not a Windows command, are you referring to a file on an NFS share or using Cygwin? It is not clear what you mean. You possibly created a file the filesystem could handle but which Windows Explorer cannot see, but without a lot more detail we cannot tell what is happening. Also, sometimes Windows Explorer needs a kick in the pants (i.e. F5 Refresh) to display a file created via a program. – Jim Garrison Mar 19 '18 at 04:47
  • I should have worded the question better. I want to ensure the file path will correspond to one the OS can handle correctly. That includes Windows Explorer not deciding not to display any files in the folder containing the file when created. Is my only option to validate the file path and filename length against known limits for each OS? Edit: Also ls is from cygwin. – Aaron Braunstein Mar 19 '18 at 04:50
  • 1
    I think the similar question exists already: https://stackoverflow.com/questions/468789/is-there-a-way-in-java-to-determine-if-a-path-is-valid-without-attempting-to-cre – Haikal Mar 19 '18 at 04:54
  • It is extremely important when you use non-standard (as in non-MS) tools to state that clearly in the post. I use and love Cygwin but it can display stuff that Explorer and CMD cannot see. Did you check the hidden/system attributes? I know the filesystem can store files with names that cannot be created in Windows standard tools. – Jim Garrison Mar 19 '18 at 04:55
  • None of the answers linked can validate the situation I tested, i.e. a file path that will bork Windows Explorer when created. At least, running Java 8 on Windows 7 (if it happens to be OS or Java version-dependent). – Aaron Braunstein Mar 19 '18 at 04:57
  • It would be really helpful if you would post the _actual_ filename you created. – Jim Garrison Mar 19 '18 at 04:59
  • Dir lists the file as well. The file path is C:\Users\abraunstein\Documents\(beta) My Maestro Repository\Datasources\abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwhipqrstuvwxyzabcdefghijlmnopqrstuvwxyz.hyper – Aaron Braunstein Mar 19 '18 at 05:01

1 Answers1

0

If the API returns SUCCESS, the file was created.

Whether or not Windows Explorer can list the file is a completely different question, and there are known edge cases where the file can be created but is not visible in Explorer or a CMD window. I don't have any examples handy at the moment but will add some when I find them.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • That makes sense, thanks. The follow up question then is, how can I validate that creating the file won't bork viewing the containing folder in Windows Explorer? – Aaron Braunstein Mar 19 '18 at 05:06
  • I'm 99% certain the answer is you cannot know via an API. Likely the only way to know will be to actually attempt it in Explorer. – Jim Garrison Mar 19 '18 at 07:38