• Post Reply Bookmark Topic Watch Topic
  • New Topic

Performance issue with Java 'File' class  RSS feed

 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tracked down the performance bottle necks in my application they all pointed to the File class. I'm recursively traversing a directory tree and for each node I call: exists(), isFile() and isDirectory(). In Sun's source code these three are just different bits of the same flag but I don't see any way to get access to the flag directly so the three separate calls gets the security manager and then gets the flag from the file system three times. On a local hard disk the performance is bearable (sort of) but on network storage these calls are taking a hundred times longer than the actual reading of a file on the network storage. (I'm on XP but I'm not sure what flavor system hosts the network storage.)


Is there any way around this?

 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use JNI but then you're stuck with one platform.
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sun's File class make use of a FileSystem class that is native BUT it is also package-private so that I can't use it directly.
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't talking about FileSystem - I was talking about JNI. You will basically write your own version of FileSystem.getBooleanAttributes:
Note that GetFileAttributes returns 0xFFFFFFFF if the attributes could not be retrieved. See GetFileAttributes
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'FileSystem' is JNI. It is also platform independent. It already ships with Java. Is there any way around the 'package-private' issue?
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not one that always works.

You can try it with reflection:
However, the calls to method.setAccessible(true) may be guarded by a security manager, so they may fail.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm recursively traversing a directory tree and for each node I call: exists(), isFile() and isDirectory().

Aside from using JNI, you should be able to get some minor improvements just by skipping some of these calls as unnecessary. For example:

The listFiles() method shouldn't be giving you any file objects for things that don't exist, after all.

Some OS's may have other objects that aren't considered files or directories. If so, you can go ahead and put in the directory check explicitly:

But you could also tell if something was really a directory by looking at the return value of listFiles(), as it will be null for any non-directory. So I might do something like this:
 
Carey Brown
Bartender
Posts: 3009
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your responses. I'll look to see if I can fit those suggestions into my application.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!