I'm writing a simple application that reads from file locations specified by the user and performs operations to the .mp3 files it finds there. I have one method called getMusicFilenames (below), which should go to the path, look at each file and, if the filename ends in .mp3, add it to an ArrayList<String> and then return the list at the end.
public static List<String> getMusicFilenames(Path p) {
List<String> listOfMusic = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
for (Path file : stream) {
if (Files.isDirectory(file)) {
if (M3UFinder.isMusicFile(file.toString())) {
listOfMusic.add(file.toString());
}
}
}
} catch (Exception e) {
System.err.println("getMusicFilenames:: error with path "
+ p + ": " + e.getMessage());
}
return listOfMusic;
}
The isMusicFile method is pretty simple, but it might be relevant:
public static boolean isMusicFile(String thisFilename) {
return thisFilename.endsWith(".mp3");
}
In testing of this method using JUnit, I set the test path to look at one particular path that contains only one .mp3 file, but the test fails saying "expected <[songtitle.mp3]> but was: <[]>. So apparently it's either not reading that the files are there, or it is reading it and just not adding it to the list. Either way, the list returns empty which causes problems for my other methods that I have written, that all depend on the list having a size and things inside of it. Have I just made some simple mistake that I can't see myself? If it helps in showing where I went wrong, the getMusicFilenames method is adapted from a similar method I was provided with, shown below.
public static List<String> getPlaylistFilenames(Path p) {
List<String> listOfPlaylists = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(p)) {
for (Path file : stream) {
if (Files.isDirectory(file, LinkOption.NOFOLLOW_LINKS)) {
listOfPlaylists.addAll(getPlaylistFilenames(file));
} else if (M3UReader.isValidHeader(file.toString())) {
listOfPlaylists.add(file.toString());
}
}
} catch (Exception e) {
System.err.println("getPlaylistFilenames:: error with path "
+ p + ": " + e.getMessage());
}
return listOfPlaylists;
}
Any help/hints greatly appreciated.