What is the best way to work with file paths in java?

I believe this is a class File

, but I have heard that it is very expensive in memory.

Is there a better way to work with file paths?

+1


source to share


4 answers


It's hard to tell without knowing what you want to do, but please don't optimize prematurely. I doubt the use of File in memory would be noticeable at all in your application.



+6


source


The File class does not contain much data by itself. It has all two instance fields. If all you're worried about is memory, this doesn't seem like a problem. Nothing is loaded from the filesystem until you open a stream or pipe.



+2


source


The File class can be expensive enough that you don't want to use it to store every file on your hard drive in memory. I know I had problems with this, especially when I tried to use the File object tree. If you are faced with a situation where using a file class is too expensive, think about how to use Strings and convert to the files you want. But the optimization that makes your program usable is probably a sign that you have more problems. It is much more likely that the overhead associated with structure placement objects will be an issue.

+2


source


The only time I know where File is using most of its memory is when you use File.list () ...

See them for some solutions:

Is there a workaround for Javas poor performance when walking around huge directories?

+1


source







All Articles