Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java.nio.files.  RSS feed

 
Steve Antoch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I need to change a file lifetime test from using LastModified to LastAccessed time.
In order to get lastAccess time for a file, I need to use something like this:

BasicFileAttributes attrs = Files.readAttributes(filePath, BasicFileAttributes.class);
In the above line, filePath is of type java.nio.file.Path

However, I have two scenarios where java.io.File works fine, but java.nio.file.Path does not:

A) the path passed in uses Windows UNC-like "\\?\" prefix to indicate a very long path,
java.nio.file.InvalidPathException: Illegal character [?] in path at index 2: \\?\D:\dev\myproj\foo \bar.txt

B) A file or directory whose name ends in a trailing space:
java.nio.file.InvalidPathException: Trailing char < > at index 17: D:\dev\myproj\foo \bar.txt

In both of these cases, the exception is thrown and there is no way to get the BasicFileAttributes so I can't retrieve the access time.

Is this a java bug?
Is there a workaround?

Thanks-


 
Tony Docherty
Bartender
Posts: 3203
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Not sure why you are seeing difference in behaviour.
Have you tried creating a File object for these paths and then used the File object's toPath() method to create a Path object.
 
Steve Antoch
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.  You get the same exception using file.toPath().  This is incredibly frustrating.

 
Tony Docherty
Bartender
Posts: 3203
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I don't know what is going on here, maybe someone else can help.

Things you could try are:

In case A if you remove the appended "\\?\" does Path handle the long file names correctly?

In case B (this is a long shot) have you tried embedding quotation marks around the whole path and/or the folder name with a trailing space?
 
Rob Spoor
Sheriff
Posts: 20895
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, Windows doesn't support trailing spaces in file or folder names. I've just tried creating and renaming folders, but whenever I use a trailing space Windows removes it when the operation has finished.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!