FileNotFoundException,该文件存在Java [英] FileNotFoundException, the file exists Java

查看:200
本文介绍了FileNotFoundException,该文件存在Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很奇怪的问题,我试图用一些Java代码和JLayer来播放一些MP3。我有一个方法设置来生成文件路径,但它给了我很多的悲伤。这里是return语句(和方法中涉及的所有代码):

pre $ private static String findSoundFile(String numSeq)
{
returnfile:/// Users / user / Desktop / FinishedPhone /+ numSeq +.mp3
}

我有一组可能~150个mp3文件,全部命名为1.mp3,2.mp3等,它们达到约156(有一些在两者之间缺失)。根据用户输入的3位数字代码,它播放其中一个声音。这个代码对于1-99之间的任何东西都是完美的,当它达到100时,它停止工作。当用户使用100或者110或者你有什么时,Java抛出一个FileNotFoundException异常。我向你保证,文件在那里。下面是使用findSoundFile返回的文件路径的代码:

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


$ / code $ / pre

当我填入空格numSeq应该填入,像这样:

pre $私有静态字符串findSoundFile(字符串numSeq)
{
返回file:///Users/user/Desktop/FinishedPhone/110.mp3;



$ b $ p
$ b

上面的代码工作正常,没有挂断就播放声音。任何想法将不胜感激,请询问是否有任何混淆。



堆栈跟踪:

  java.io.FileNotFoundException:/Users/user/Desktop/FinishedPhone/111.mp3(没有这样的文件或目录)
在java.io.FileInputStream.open(本地方法)$ b (FileInputStream.java:120)
在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.java:1010)
在SoundPlayer.play(SoundPlayer.java:26)
在SerialProcessor.processNumberSequence(SerialProcessor.java:37)
在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其中一个档案:

  -rw-r  -  rw- 1位使用者432923 Feb 27 14:15 /Users/user/Desktop/FinishedPhone/111.mp3 




ls -l 100以下的一个:
$ b

  -rw -r -rw- 1用户人员480570 Feb 25 20:43 /Users/user/Desktop/FinishedPhone/99.mp3 


解决方案

你的numSeq有问题。尝试像这样修剪



$ p $ returnfile:/// Users / user / Desktop / FinishedPhone /+ numSeq.trim ()+.mp3


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

解决方案

Your numSeq has problem. Try to trim it like this

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

这篇关于FileNotFoundException,该文件存在Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆