tags:

views:

184

answers:

4

I believe it's the File class but I heard that is very expensive in memory.

Is there a better way to work with file paths?

+6  A: 

It's hard to say without knowing what you want to do, but please do not prematurely optimize. I doubt the memory use of a File object will be at all noticeable in your application.

Relic
+2  A: 

The File class doesn't hold much data in and of itself. It has all of two instance fields. If all you're worried about is memory, it doesn't look like it's much of a problem. Nothing is loaded from the file system till you open a stream or a channel.

sblundy
+2  A: 

The File class might be expensive enough that you don't want to use it in order to store every file on your hard drive in memory. I know I've had issues with that, particularly when I tried to use a tree of File objects. If you do encounter a situation where using the file class is too expensive, consider just using Strings, and converting to Files at need. But having that be the optimization that makes your program become practical is probably a sign that you have bigger issues. It is far more likely to have the overhead associated with the structure holding objects to be an issue.

Brian
+1  A: 

The only time I know where File uses a lot of memory is when you use File.list()...

See these for some solutions:

Is there a workaround for Java’s poor performance on walking huge directories?

Pyrolistical