0

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.

1
  • 1
    What is this line meant for "if (Files.isDirectory(file)) {" Commented Dec 11, 2014 at 14:42

1 Answer 1

2

You are missing an exclamation mark here if (Files.isDirectory(file)).

It should be if (!Files.isDirectory(file))

Sign up to request clarification or add additional context in comments.

2 Comments

That's worked, it's also shown me that I currently have it where it stores the whole location instead of just the last part of the name, which I probably should have known but for some reason didn't. Say I just wanted the name, how would I go about getting that? Would it just be creating substrings starting at the last "\"?
Try replacing file.toString() with file.getFilename().toString()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.