简体   繁体   English

Java在资源文件夹中写入.txt文件

[英]Java write .txt file in resource folder

I am trying to write a .txt file in a resource folder but it doesn't write anything.我正在尝试在资源文件夹中写入一个 .txt 文件,但它没有写入任何内容。 I am able to read the same .txt file by doing:我可以通过执行以下操作读取相同的 .txt 文件:

Scanner scanner = null;
InputStream IS = MyClass.class.getResourceAsStream("/File/My FileH");
scanner = new Scanner(IS);

But when it comes to write, I have tried doing:但是在写作方面,我曾尝试这样做:

PrintWriter writer =  new PrintWriter(
    new File(this.getClass().getResource("/File/My FileH").getFile()));
writer.println("hello");
writer.close();

Any suggestions on how to write in that folder?关于如何在该文件夹中写入的任何建议?

You can't write something in to a resource, assume that you packed your resource as a jar.您不能在资源中写入内容,假设您将资源打包为 jar。 Jar is only read only . Jar 是read only You can't update that.你不能更新那个。 Either you can extract the jar and edit the contents.您可以提取jar并编辑内容。

You can try Preferences as an alternative您可以尝试使用首选项作为替代方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM