FileNotFoundException, the file exists Java [closed]

僤鯓⒐⒋嵵緔 提交于 2019-11-29 16:07:28

Your numSeq has problem. Try to trim it like this

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

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

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