• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why Is Java Saying these Files Are of Zero Length?

 
Ranch Hand
Posts: 249
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 5558
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you type: D:,
then you get into the current directory in D, and that may not be D:\Bug. Perhaps that is the problem?
 
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other than renaming some variables, I ran your code as-is, and it seemed to work as your wanted:

Windows 10 and Java 17.0.7
 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kevin Simonson
Ranch Hand
Posts: 249
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:Other than renaming some variables, I ran your code as-is, and it seemed to work as your wanted:

Windows 10 and Java 17.0.7



Ron, I just changed the name of your class from (MyDir) to (McLeod), giving me:

But I get the same results:

My command "j8" is just an alias for "\Langs\Java\8u40-windows-x64\bin\java".
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java-8 dates back to 2014. Try something newer.
 
Ron McLeod
Sheriff
Posts: 4641
582
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have JDK 8u40 installed anywhere, but I tried using 8u202 on Windows 10 Pro v22H2 build 19045.2965 and I got the same (successful) results.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic