内容更改后如何重新读取文本文件的新内容?

How can I re-read the new contents of a text file after the contents have changed?

我想在内容更改后读取文本文件,但Java正在缓存文件的原始内容。

我试过 URLConnection.setUsesCaches(false),但没有用。我还尝试添加一个不断变化的虚拟查询字符串参数,但同样没有帮助

Resource[] resources = applicationContext.getResources("classpath*:/" + file);

for (Resource resource:resources) {
    URL url = resource.getURL();
//   reconstruct the URL to remove any possible vestiges of the resource
    url = new URL(url.toString());
    URLConnection conn = url.openConnection();
    conn.setUseCaches(false); // Tried this first, but it did not work!
//    url = new URL(url.toString() + "?dummy=" + id); // tried this also, but it doesn't work at all
    InputStream inputStream = conn.getInputStream();

有什么方法可以告诉 Java 重新读取文件的新内容吗?

当你在 IDE 上启动你的 Spring 时,类 被编译到一个特定的输出文件夹(在你的例子中是 target 文件夹),静态资源也是复制到各自的文件夹。

这意味着在启动应用程序时保持不变且不可变,您需要手动将文件复制到目标文件夹(/target/classes/my-file.html)以更新它(Spring仅在该文件夹中查找文件目标文件夹)。

但是让我们重新考虑你的实现,当你尝试更新静态资源文件时,从一开始就是错误的。