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?
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?
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.
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.
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?