Search code examples
javafile-iomp3filenotfoundexceptionjlayer

FileNotFoundException, the file exists Java


I have a very strange issue, I'm trying to play some MP3s with some Java code and JLayer. I have a method setup to generate the file path, but it's giving me a ton of grief. Here is the return statement (and all the code involved in the method):

private static String findSoundFile(String numSeq)
{
    return "file:///Users/user/Desktop/FinishedPhone/" + numSeq + ".mp3"
}

I have a set of maybe ~150 mp3 files, all named 1.mp3, 2.mp3 etc. They go up to about 156 (there's some missing in between). Based on user input of a 3 digit code, it plays one of the sounds. This code works flawlessly for anything between 1-99, its when you get to 100 where it stops working. When the user punches in 100 or 110 or what have you, Java throws a FileNotFoundException. I assure you, the file is there. Here is the code that uses the filepath returned by findSoundFile:

public static void processNumberSequence(String numSeq) throws IOException
{
    if (numSeq != "")
    {
        String soundLoc = findSoundFile(numSeq);
        File file = new File(soundLoc);
        System.out.println("System can read: " + file.canRead());
        System.out.println(soundLoc);
        SoundPlayer soundToPlay = new SoundPlayer(soundLoc);
        soundToPlay.play();
    }
}

It gets weirder, when I fill in the space that numSeq is supposed to fill in, like this:

private static String findSoundFile(String numSeq)
{
    return "file:///Users/user/Desktop/FinishedPhone/110.mp3";
}

The above code, works fine, plays the sound without hang up. Any ideas would be greatly appreciated, and please ask if there's any confusion.

The stacktrace:

java.io.FileNotFoundException: /Users/user/Desktop/FinishedPhone/111.mp3 (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at java.net.URL.openStream(URL.java:1010)
at SoundPlayer.play(SoundPlayer.java:26)
at SerialProcessor.processNumberSequence(SerialProcessor.java:37)
at SerialTest.serialEvent(SerialTest.java:98)
at gnu.io.RXTXPort.sendEvent(RXTXPort.java:732)
at gnu.io.RXTXPort.eventLoop(Native Method)
at gnu.io.RXTXPort$MonitorThread.run(RXTXPort.java:1575)

ls -l of one of the files:

-rw-r--rw-  1 user  staff  432923 Feb 27 14:15 /Users/user/Desktop/FinishedPhone/111.mp3

ls -l for one under 100:

-rw-r--rw-  1 user  staff  480570 Feb 25 20:43 /Users/user/Desktop/FinishedPhone/99.mp3

Solution

  • Your numSeq has problem. Try to trim it like this

     return "file:///Users/user/Desktop/FinishedPhone/" + numSeq.trim() + ".mp3