FileNotFoundException, the file exists Java [closed]

眉间皱痕 提交于 2019-11-28 09:56:23

问题


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

回答1:


Your numSeq has problem. Try to trim it like this

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



回答2:


Well, I guess the problem come with the fact that you are using file:// scheme in front the path. I don't know exactly how the File(String pathname) behaves when using this scheme. The File class takes many constructors and particuly this one File(URI uri) whose Javadoc says :

Creates a new File instance by converting the given file: URI into an abstract pathname.

So, in my opinion, you should use this constructor instead of the previous one. Let me show you some code that proves what I say :

    public class FileTest {

    /**
     * @param args
     * @throws URISyntaxException 
     */
    public static void main(String[] args) throws URISyntaxException {
        // TODO Auto-generated method stub
        String pathWithNoScheme = "/home/dimitri/workspace/Coursera/collinear/input6.txt";
        String pathWithScheme = "file://" + pathWithNoScheme;
        URI uri = new URI(pathWithScheme);


        File fileWithNoScheme = new File(pathWithNoScheme);
        System.out.println(fileWithNoScheme.canRead()); //returns true

        File fileWithScheme = new File(uri);
        System.out.println(fileWithScheme.canRead()); //returns true

        fileWithNoScheme = new File(pathWithScheme);
        System.out.println(fileWithNoScheme.canRead()); //returns false

    }

}

As you see the when passing a file:// scheme to the File(String pathName) constructor, it returns false but when I pass it to an URI, it returns true.

So change findSoundFile to return an URI instead of a String or wrapped the return value of this method to an URI



来源:https://stackoverflow.com/questions/15350392/filenotfoundexception-the-file-exists-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!