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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36130111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 00:59:14  来源:igfitidea点击:

Java write .txt file in resource folder

java

提问by LPV

I am trying to write a .txt file in a resource folder but it doesn't write anything. I am able to read the same .txt file by doing:

我正在尝试在资源文件夹中写入一个 .txt 文件,但它什么也没写。我可以通过执行以下操作读取相同的 .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?

关于如何在该文件夹中写入的任何建议?

回答by Viswanath Lekshmanan

You can't write something in to a resource, assume that you packed your resource as a jar. Jar is only read only. You can't update that. Either you can extract the jarand edit the contents.

您不能在资源中写入内容,假设您将资源打包为 jar。罐子是唯一的read only。你不能更新那个。您可以提取jar并编辑内容。

You can try Preferencesas an alternative

您可以尝试使用首选项作为替代