0

さて、私のコードは以下の通りです。常に例外がある理由を知りたいです。mp3 ファイルは、test.java ファイルと同じディレクトリにあります。私は何を間違っていますか?また、たとえば私の音楽ライブラリから mp3 ファイルを読み取るにはどうすればよいですか : path - Libraries\Music

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プロセスを実行するのと同じディレクトリ」とはどういう意味ですか? 例を教えてください。

4

3 に答える 3

4

これを印刷する:

  System.out.println("File not found.");

これを考えると、誤解を招く:

catch (UnsupportedTagException | InvalidDataException | IOException e)

例外をダンプする必要があります(e.printStackTrace()実際の問​​題を特定するため)。

ファイル.mp3はファイルと同じディレクトリにあり.javaます。しかし、それは関係ありません。Javaプロセスを実行する場所と同じディレクトリにありますか? それが必要な場所です。

例えば

$ cd /mydir
$ java com.whatever.TestJava 

上記では、ファイルがディレクトリ.mp3にある必要があります/mydir

于 2012-11-30T11:00:09.913 に答える
1

@BrianAgnew で述べたように、例外をダンプする必要があります。

UPDATE これを試して、使用したいファイルを選択してください:

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();
    }
}
}
于 2012-11-30T11:07:16.420 に答える
0

解決しました!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 に乾杯します。

于 2012-11-30T13:09:50.043 に答える