简体   繁体   English

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

[英]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.

相关问题 如何读取文件内容,写入outputstream以及重新读取并存储在Java内存中的字符串变量中 - how to read contents of file, write to outputstream and the re-read and Store in a string variable in memory in java 如何读取文本文件的内容并检查它们在 Java 中是否有效 - How can I read the contents of a text file and check if they're valid or invalid in Java 如何读取文本文件的内容并将其复制到 char[][] 数组? - How can I read and copy the contents of a text file to a char[][] array? 如何更新文本文件的内容 - How can I update contents of a text file 如何将字符串中的新行字符插入PrintStream,然后使用扫描程序重新读取该文件 - how to insert a new line character in a string to PrintStream then use a scanner to re-read the file 如何读取文件夹中存在的所有 Zip 文件并创建包含所有内容的新文本文件 - How do i read all the Zip files present in the folder and create a new text file with all the contents 调用IOUtils.copy后如何重新读取InputStream? - How to re-read an InputStream after calling IOUtils.copy? 如何创建一个休眠集合,每次我请求时都将重新读取它? - How can I create a hibernate collection that will be re-read every time I request it? 如何将 txt 文件的内容读取到文本区域? - How do I read the contents of a txt file to a text area? (Java)如何读取可以使用各种编码的文本文件并将内容输出到看起来正常的文本文件中? - (Java) How can I read in a text file that could use various encodings and output the contents in a text file that looks normal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM