运行音频指纹对比Java代码时出现“找不到文件或目录”错误,如何解决?
Hey there, let's work through that frustrating "file or directory not found" error you're hitting with your audio fingerprinting code! I've gone through your code and the setup, so here are the key fixes and checks to get things running smoothly:
1. Verify Your File Path Structure
First up, let's make sure your files are where your code expects them to be:
- If this is a standard Java project, your
res/rawfolder needs to live in the root directory of your project (at the same level as yoursrcfolder), not nested insidesrc. If it's in the wrong place, the JVM won't be able to locate your WAV files. - To rule out path confusion entirely, test with an absolute file path first. For example:
If this works, your relative path was the issue—adjust your folder structure or path accordingly.String songA = "/Users/your-username/your-project-folder/res/raw/canon_d_major.wav";
2. Double-Check Filename Spelling & Case
Operating systems like Linux and macOS are case-sensitive, so make sure:
- The filenames match exactly what's in your code:
canon_d_major.wavandcock_a.1.wav(pay close attention to the.1in the second filename—easy to miss a typo here!). - Even on Windows (which is case-insensitive), typos in the filename (like
canon_d_major.WAVinstead of.wav) can still cause issues.
3. Confirm the Files Actually Exist
It sounds obvious, but it's easy to overlook:
- Navigate to your
res/rawfolder manually and check that all three WAV files are present. Sometimes files get deleted accidentally, or fail to copy over correctly when setting up the project.
4. Fix the Variable Name Bug in Your Code
While this isn't causing the file error, you've got a small typo that will break your code once the file issue is fixed: you're using similarity to call methods, but your variables are named similarity1 and similarity2. Here's the corrected code:
public static void main(String[] args) { String songA = "res/raw/canon_d_major.wav"; String songB = "res/raw/canon_d_major.wav"; Wave waveA = new Wave(songA); Wave waveB = new Wave(songB); String recordedClip = "res/raw/cock_a.1.wav"; Wave waveRec = new Wave(recordedClip); FingerprintSimilarity similarity1, similarity2; similarity1 = waveA.getFingerprintSimilarity(waveRec); System.out.println("clip is found at " + similarity1.getsetMostSimilarTimePosition() + "s in " + songA + " with similarity " + similarity1.getSimilarity()); similarity2 = waveB.getFingerprintSimilarity(waveRec); System.out.println("clip is found at " + similarity2.getsetMostSimilarTimePosition() + "s in " + songB + " with similarity " + similarity2.getSimilarity()); }
Start with the file path checks first—those are the most common culprits here. Once you get past the file error, the variable name fix will ensure your output works as expected!
内容的提问来源于stack exchange,提问作者Yosra Bader

