I’m trying to do the following: I’ve a database filled with file names located under a directory. This directory is changing constantly (downloaded files are being added and removed). My application is supposed to scan this directory for the first time and add the files into the database. The second time the application will run, it needs to check if the filenames in the database are still available in the directory.
For the check I use the following pseudo code:
get the filename from the database
check if exists (file f = new File(filename))
if (f.exists()){
mark as existing;
} else {
mark is as deleted
}
if it does, then mark it as existing, else mark it as removed (later will clean the database up)
The question is: How can I check all the files on the database if they exists without producing much garbage? Files can be more than 1000. Running the loop with “new File(…)” more than 1000 times will cause too much garbage.
Any help is appreciated.
The
File()object is really tiny. It has only path string in it and reference to the FileSystem object. It just look like a wasting resources, but it’s not.Think about
Fileobject as a path String with few helper methods to deal with file paths.It has nothing to do with file descriptor or other heavy resources.
Never do optimization before profiling. You will end up with non optimal difficult to maintain code.