英文:
Why can't Java find a file inside of the main source folder?
问题
我正在制作一个游戏,但 Java 似乎找不到我的文件。
我的播放声音函数是:
void playSound(String soundLocation) {
try {
File soundPath = new File(soundLocation);
if (soundPath.exists()) {
AudioInputStream audioInput = AudioSystem.getAudioInputStream(soundPath);
Clip clip = AudioSystem.getClip();
clip.open(audioInput); // 添加这行以打开音频流
clip.start();
JOptionPane.showMessageDialog(null, "Press OK to stop playing");
} else {
System.out.println("Can't find file");
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
在我的主类中我有:
package game;
public class GUI_Handler {
public static void main(String[] args) {
String soundPath = "c://users/harry/java workspace/SlavaSourcecode/src/game/jump.wav";
SOUND_Handler soundStuff = new SOUND_Handler();
soundStuff.playSound(soundPath);
}
}
它会输出 "Can't find file"。
请帮忙看看!谢谢,Harry
英文:
I'm working on a game, and Java can't seem to find my file.
My play sound function is :
void playSound(String soundLocation) {
try
{
File soundPath = new File(soundLocation);
if (soundPath.exists()) {
AudioInputStream audioInput = AudioSystem.getAudioInputStream(soundPath);
Clip clip = AudioSystem.getClip();
clip.start();
JOptionPane.showMessageDialog(null, "Press OK to stop playing");
} else {
System.out.println("Can't find file");
}
}
catch(Exception ex) {
ex.printStackTrace();
}
}
In my main class I have :
package game;
public class GUI_Handler {
public static void main(String[] args) {
String soundPath = "c://users/harry/java workspace/SlavaSourcecode/src/game/jump.wav";
SOUND_Handler soundStuff = new SOUND_Handler();
soundStuff.playSound(soundPath);
}
}
It outputs " Can't find file");
Please help! Thanks,
Harry
答案1
得分: 1
你必须将所有的 /
替换为 \
,但在Java中,你还必须用另一个反斜杠对反斜杠进行转义,以防止它被视为特殊字符。
因此,一旦你用 \
替换了所有的 /
,你还需要将所有的 \
替换为 \\
:
如果你采用你已经有的内容,你可以修改 soundPath
如下:
soundPath.replace("//","\\").replace("\\","\\\\");
然后它应该可以工作。
或者你可以手动编辑路径为:
"c:\\users\\harry\\java workspace\\SlavaSourcecode\\src\\game\\jump.wav";
但请注意,这在其他机器上不太适用。实际上,更好的做法是使用构建工具(Gradle/Maven),它具有标准的文件结构(大多数开发者现在使用的“约定”)。文件结构有两个目录:一个用于源代码 src/main/java
,另一个用于资源 src/main/resources
。通常你会把这些资源放在 resources
目录中,并从资源目录中加载 jump.wav
,它在类路径上始终是相对可访问的。
英文:
You must replace all /
with \
but in Java you must also escape the backslash with another backslash to prevent it being treated as a special character.
So, once you've replaced the /
s with \
you then need to replace all \
with \\
:
If you take what you already have you can amend soundPath
to:
soundPath.replace("//","\").replace("\","\\");
And it should work.
Or you can manually edit the path to:
"c:\\users\\harry\\java workspace\\SlavaSourcecode\\src\\game\\jump.wav"
Note though that this is not at all portable to other machines. In reality what would be better is if you used a build tool (Gradle/Maven) which has a standard file structure (a "convention" used by most developers these days). The file structure has two directories: one for source code src/main/java
, and another for resources src/main/resources
. Typically you'd stick any such assets in the resources
directory and load the jump.wav
from the resources dir which is always accessible relatively on the classpath.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论