我的资源文件夹中有一个 .properties 文件。我想加载该文件,但是当我使用 IOUtils 读取文件内容时,它只会创建一个新的空文件,并且不会复制 test.properties 的内容。
try (InputStream demoTemplate =
MyClass.class
.getClassLoader()
.getResourceAsStream("/config/test.properties")
{
IOUtils.copy(demoTemplate, new FileWriter(destinationFile));
LOGGER.debug(" demo file content: " + OUtils.toString(demoTemplate));
}
我的日志输出为空,将创建文件,但它没有内容。
当我更改 test.properties 文件的路径时,它将导致 FileNotFound
异常,因此我确信 ClassLoader 可以加载资源。 test.properties
文件包含数据。
最佳答案
您的日志输出为空,因为您已用尽 InputStream
进入目标文件。
您的输出可能为空,因为 FileWriter
从未正确冲洗或关闭。
也许您可以读取目标文件并记录它,再次打开原始源,保留输入流内容的副本,或者在传输数据时进行记录,尽管这不需要使用简单的 IOUtils
helper 。
关于java - getResourceAsStream() 找到资源,但会写入一个空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31880194/