Get path of a file from a subdirectory of a directory in CLASSPATH
Given the following directory structure:
working-directory
subfolder1
file1.wav
file2.wav
file3.wav
subfolder2
file4.wav
file5.wav
开发者_如何转开发 file6.wav
subfolder3
file7.wav
file8.wav
file9.wav
jar-that-im-running.jar
I need to get the path to each wav file. I figured that since the folder is in the working directory, and is thus part of the classpath (if my assumption is wrong, I'd just add the working directory to the classpath), I could just run:
String path = ClassLoader.getSystemResource("file1.wav");
or
String path = ClassLoader.getSystemResource("/file1.wav");
but it wouldn't work, unless I specified the folder the wave file was in. This would be fine, but I wouldn't know what folder each wave file is in; I only know their names. I'm going to process all the files one way or another, but the order that I do depends on a config file. Also, I am not going to edit these files directly. Instead, I'm going to be passing them off as arguments to a ProcessBuilder. Since some of the directories in the path may have spaces, which get converted to %20
in URLs, I figured I could convert them with path.replaceAll("%20", " ")
. Will I be better off using files, or is there a way to get a specific wav file, without knowing its parent folder.
Did you try getting from the class loader as a system resource? Here's a snippet of code to illustrate:
String path = ClassLoader.getSystemResource("subfolder1/file1.wav");
精彩评论