java File.separator 在 Windows 上的文件路径中变为“%”

var*_*en_ 1 java windows file

我尝试从资源文件夹中读取文件。问题是,在 Windows 上File.separator变成了“ %"

String inputFilesFolder = "input_files" + File.separator;
File file = new File(classLoader.getResource(inputFilesFolder + "filename").getFile());
Run Code Online (Sandbox Code Playgroud)

inputFilesFolder仍然是罚款(input_files/),但在创建文件之后file.getPath()变得D:\blabla\input_files%filename

然后我尝试读取文件,但我收到 FileNotFoundException(大惊喜)。这里有什么问题?

Meh*_*egh 5

File.separator 是文件系统的东西。当您使用 classLoader.getResource() 时,请始终使用正斜杠,因为资源的名称是以“/”分隔的路径名。

有关 getResource() 的信息,请参阅 Javadoc