I'm working on a computer with operating system Windows 10 Enterprise. On my D: drive in directory "Bug" I have two files, "Oops.Txt" and "Qf.Txt", of lengths 6 and 49 respectively. When I type in "D:", it takes me directly to that "\Bug" directory. (In other words, when I was last on my D: drive I was in that directory.) When I type "dir D:" I get a listing of those two files, and when I type in "dir D:\Bug" I get an identical listing of those two files.
Now I've written file "MyDir.java" which consists of:
When I run it I get:
Why am I getting zero lengths for those two files when I execute "j8 MyDir D:"? It's getting the two files that are in my "Bug" directory. Why is it getting the two sizes wrong? Is there a way to fix my code so that I can execute "j8 MyDir D:" and get the correct results?
Kevin, what is this "j8" command that you're running?
I'm guessing it may be an alias for a Java 8 runtime. If so, can you try any other JDKs? Something more modern? This may be something that's been fixed in other versions.
It's dagngerous to assume what the current directory or even the current logged Windows drive is in Java. Other parts of the system might alter it without warning. Always use an
absolute path.
Mike, I believe "J8" is an IBM JVM. I've worked with J9, so the main puzzlement is whether that's a really old JVM or they just numbered arbitrarily.
Education won't help those who are proudly and willfully ignorant. They'll literally rather die before changing.
The documentation of File.length() implies that 0L is returned whenever the length could not be determined. Try using Files.readAttributes(child.toPath(), BasicFileAttributes.class) and you'll probably see an exception instead.
Aside from trying a more modern JDK, don't overlook Ron's previous comment about how the documentation for File.length() may explain this, and recommends using Files.readAttributes() to see if there's an error message. You can go further and print out additional diagnostic info that may help:
So you made a portal in time and started grabbing people. This tiny ad thinks that's rude:
Gift giving made easy with the permaculture playing cards