好吧,我的代码如下。我想知道为什么总是有例外。mp3 文件与 test.java 文件位于同一目录中。我做错了什么?另外,如何从我的音乐库中读取mp3文件:路径 - 库\音乐
import java.io.IOException;
import com.mpatric.mp3agic.ID3v1;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
public class test
{
public static void main(String args[])
{
Mp3File mp3file = null;
try {
mp3file = new Mp3File("dom.mp3");
} catch (UnsupportedTagException | InvalidDataException | IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
System.out.println("File not found.");
}
if (mp3file.hasId3v1Tag()) {
ID3v1 id3v1Tag = mp3file.getId3v1Tag();
System.out.println("Track: " + id3v1Tag.getTrack());
System.out.println("Artist: " + id3v1Tag.getArtist());
System.out.println("Title: " + id3v1Tag.getTitle());
System.out.println("Album: " + id3v1Tag.getAlbum());
System.out.println("Year: " + id3v1Tag.getYear());
System.out.println("Genre: " + id3v1Tag.getGenre() + " (" + id3v1Tag.getGenreDescription() + ")");
System.out.println("Comment: " + id3v1Tag.getComment());
}
}
}
例外
java.io.FileNotFoundException: File not found dom.mp3
at com.mpatric.mp3agic.FileWrapper.init(FileWrapper.java:26)
at com.mpatric.mp3agic.FileWrapper.<init>(FileWrapper.java:19)
at com.mpatric.mp3agic.Mp3File.<init>(Mp3File.java:53)
at com.mpatric.mp3agic.Mp3File.<init>(Mp3File.java:41)
at test.main(test.java:13)
File not found.
Exception in thread "main" java.lang.NullPointerException at test.main(test.java:19)
mpatric软件包是第三方。我猜这工作正常。
"与运行 Java 进程的目录相同"是什么意思?你能给我举个例子吗?
打印此内容:
System.out.println("File not found.");
具有误导性,因为:
catch (UnsupportedTagException | InvalidDataException | IOException e)
您需要转储异常(e.printStackTrace()
以确定真正的问题)。
您的.mp3
文件与.java
文件位于同一目录中。但这无关紧要。它是否与您运行 java 进程的位置位于同一目录中?这就是它需要的地方。
例如
$ cd /mydir
$ java com.whatever.TestJava
在上面,您的.mp3
文件需要在/mydir
目录中
正如@BrianAgnew所提到的,你应该转储你的异常。
更新试试这个并选择您要使用的文件:
public class Test
{
public static void main(String args[])
{
Mp3File mp3file = null;
try {
JFileChooser jfc = new JFileChooser();
int fileResult = jfc.showOpenDialog(null);
if (fileResult == JFileChooser.APPROVE_OPTION) {
String path = jfc.getSelectedFile().getPath();
mp3file = new Mp3File(path);
if (mp3file!=null && mp3file.hasId3v1Tag()) {
ID3v1 id3v1Tag = mp3file.getId3v1Tag();
System.out.println("Track: " + id3v1Tag.getTrack());
System.out.println("Artist: " + id3v1Tag.getArtist());
System.out.println("Title: " + id3v1Tag.getTitle());
System.out.println("Album: " + id3v1Tag.getAlbum());
System.out.println("Year: " + id3v1Tag.getYear());
System.out.println("Genre: " + id3v1Tag.getGenre() + "("+id3v1Tag.getGenreDescription() + ")");
System.out.println("Comment: " + id3v1Tag.getComment());
} else {
System.out.println("The mp3 file does not exists or does not have a ID3v1Tag");
}
}
} catch (UnsupportedTagException | InvalidDataException | IOException e) {
System.err.println("File not found.");
e.printStackTrace();
}
}
}
解决了!System.out.println(System.getProperty("user.dir"));将其粘贴到我的代码中,找出了根目录。显然,它与src和bin文件夹处于同一级别。将文件粘贴到那里,它现在就像一个魅力。
好吧,如果有人想知道您是否可以更改主目录,则不能。
如果要访问另一个文件夹,则必须诉诸目录遍历。假设您的音乐文件"Ride the Lightning.mp3"在C:\Users"Your Username"\Music\
然后要阅读,您必须执行以下操作:
mp3file = new Mp3File("../../../Music/Misc/Ride the Lightning.mp3");
为Brian和Chasmo的有用帖子干杯。