gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:57:44 27 4
gpt4 key购买 nike

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

String inputFilesFolder = "input_files" + File.separator;
File file = new File(classLoader.getResource(inputFilesFolder + "filename").getFile());

inputFilesFolder 仍然没问题 (input_files/),但创建文件后 file.getPath() 变为 D:\blabla\input_files%文件名

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

最佳答案

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

See Javadoc for getResource()

关于java File.separator 在 Windows 上的文件路径中变为 "%",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30415144/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com