写入 Java 包内的文件
嗨,我是 Java 新手,请耐心等待。 我正在从文件中读取信息并想写入它。我正在使用 eclipse。
我的目录结构是
ProjectName-->src-->Package
(这是我的 java 文件所在的包)
我正在尝试创建一个 jar 文件。我的文本文件位于包内。
要从文件中读取,我使用下面的代码。
InputStream is = getClass().getResourceAsStream("BookingInfo.csv");
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
现在我想写入同一个文件。我正在做类似的事情
String filename ="src/Package/BookingInfo.csv";
File myFile = new File(filename);
fw = new FileWriter(myFile,true);
fw.write(report);
fw.close();
但不知何故它没有更新它。你能就此给我建议吗?
Hi I am new to Java, so kindly bear me.
I am reading info from a file and want to write to it.I am using eclipse.
My directory structure is
ProjectName-->src-->Package
(This is my package where my java files are)
I am trying to create a jar file. My text file is inside the package.
To read from file i am using below code.
InputStream is = getClass().getResourceAsStream("BookingInfo.csv");
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
Now i want to write to same file.I am doing something like this
String filename ="src/Package/BookingInfo.csv";
File myFile = new File(filename);
fw = new FileWriter(myFile,true);
fw.write(report);
fw.close();
But somehow its not updating it. Can you kindly adivise me on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,您将无法写回 .jar 文件。您可以改为写入文件系统中的文件,但 .jar 将是不可变的。
您可以以编程方式解压 .jar(到临时目录中),覆盖该文件,然后重建 .jar 文件。请参阅有关 .jar 相关 API 的文档。
You won't be able to write back into the .jar file, unfortunately. You can write instead to a file in the filesystem, but the .jar will be immutable.
You could unpack the .jar programmatically (into a temp directory), overwrite the file and then rebuild the .jar file. See the documentation on the .jar-related APIs.
您的项目的工作区文件不会自动同步,因此您必须在 Eclipse 中刷新。然后你就会看到变化。
Your project's workspace files are not syncronized automatically, so you have to refresh in Eclipse. Than you will see the changes.
您需要在项目内创建新文件时提供完整路径。试试这个代码:
File xmlFile = new File("src/com/company/project/xml/tags.xml");
您将能够在 com.company.project.xml 包中创建文件。
You need to give full path while creating a new file inside a project. Try this code:
File xmlFile = new File("src/com/company/project/xml/tags.xml");
And you will be able to create file in com.company.project.xml package.
怎么样:
What about: