[英]How can I re-read the new contents of a text file after the contents have changed?
I would like to read a text file after the contents have changed, but Java is caching the original contents of the file.我想在内容更改后读取文本文件,但 Java 正在缓存文件的原始内容。
I have tried URLConnection.setUsesCaches(false), which didn't help.我试过 URLConnection.setUsesCaches(false),但没有帮助。 I also tried adding a changing dummy query string parameter, but again no help
我也尝试添加一个不断变化的虚拟查询字符串参数,但同样没有帮助
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();
Is there any way to tell Java to re-read the new contents of a file?有没有办法告诉 Java 重新读取文件的新内容?
When you start your Spring on IDE, the classes are compiled to a specific output folder ( target
folder in your case), static resources are also copied to respective folder. When you start your Spring on IDE, the classes are compiled to a specific output folder (
target
folder in your case), static resources are also copied to respective folder.
It mean to be constant and immutable when start the app, you need to manual copy your file to target folder ( /target/classes/my-file.html
) to have it updated (Spring only look for files in that target folder).这意味着在启动应用程序时保持不变且不可变,您需要手动将文件复制到目标文件夹(
/target/classes/my-file.html
)以更新它(Spring 仅查找该目标文件夹中的文件)。
But let re-think your implementation, it would be wrong from begining when you tried to update a static resource file.但是让我们重新考虑一下您的实现,当您尝试更新 static 资源文件时,这将是错误的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.