Answer the question
In order to leave comments, you need to log in
How to speed up reading file properties in JAVA?
There is an ArrayList ( paths ) with addresses of mp3 files (9k elements). The code reads the properties of each, namely its duration. The reading time of all takes 12 minutes.
for (String s : paths) {
File file = new File(s);
AudioFileFormat baseFileFormat = new MpegAudioFileReader().getAudioFileFormat(file);
Map properties = baseFileFormat.properties();
System.out.println(properties.get("duration"));
}
Answer the question
In order to leave comments, you need to log in
If this is not just an abstract piece of code, but part of a certain System that works with audio files, then I would advise you to read the properties not at the time of accessing the list of files, but at the moment the file is added to the System. The duration of an audio track is taken not from the properties of the file, but from its contents. This requires opening the file for reading, parsing the MPEG header and searching for the required data. No matter how you optimize this process, if you do it with every user request, the file system will "lie down". It would be much more efficient to analyze the audio at the moment it is loaded into the System. Surely you have some kind of file registry stored in the DBMS (and if not, it is better to create it), and store all the necessary information about the file there. For example, at the first start of the System, it scans the specified directories with audio files, takes a long time to extract the necessary information from them and saves it in its database. In the future, there are two options for updating data:
1. Rescanning, during which the list of files on the disk is first checked against the list in the database (quick operation), and then only deleted/added files are analyzed and the list in the database is updated.
2. If all further operations are carried out only by means of your System (and not to go into the folder with OS audio tools), then with each operation of adding / deleting a file, the System will update the entry in the database table.
PS I remember, once upon a time, in Windows XP I tried to open a folder with 10 thousand files in Explorer, it was "painful"...
well, I can't say that I'm a very big expert, but try not to create objects on every iteration of the loop. Something like this:
MpegAudioFileReader reader = new MpegAudioFileReader();
AudioFileFormat baseFileFormat = new MpegAudioFileReader();
Map properties;
for (String s : paths) {
File file = new File(s);
baseFileFormat = reader.getAudioFileFormat(file);
properties = baseFileFormat.properties();
System.out.println(properties.get("duration"));
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question