如何将文本输出到资源文件夹Maven中的文件 [英] How to output text to a file in resource folder Maven
问题描述
这是我的maven项目的结构:
主和测试文件夹在src文件夹下,java和resources文件夹在主文件夹下,
资源文件夹,有一个csv文件准备阅读。
src
--main
| - java
| - resources
| - test.csv
test
目前,我试图使用supercsv库覆盖test.csv文件。
我知道 public CsvMapWriter(Writer writer,CsvPreference preference)
所以,我想使用OutputStreamWriter,但是如何使用:
InputStreamReader reader = new InputStreamReader(getClass()。getClassLoader()。getResourceAsStream(acacia_implexa.csv));
mapReader = new CsvMapReader(reader,CsvPreference.STANDARD_PREFERENCE);
到 resources
文件夹中的文件,但过程有点单调乏味。
- 告诉Maven 将您的
资源
文件夹的位置写入文件 - 读取Java中
资源
文件夹的位置 - 使用该位置写入您的档案
注意
-
为什么要指定
资源
文件夹文件的位置?是不是src / main / resources
总是?如果你有一个嵌套的模块结构,如rootModule
├─childModule1
│└─ src
│└─main
│└─resources
└─childModule2
└─...
那么相对路径
src / main / resources
解析为parentModule / src / main / resources
不存在。 - 将文件写入
资源
将不能通过Class.getResource()
访问。Class.getResource()
为您提供对位于类路径中的文件的读取访问。在编译期间,位于resources
文件夹中的文件会复制到二进制输出文件夹(读类路径)。为了在Class.getResource()
的帮助下访问您新创建的资源文件,您必须重新编译&重新启动应用程序。
指南
- 向Maven pom添加
RESOURCE_PATH
属性,并告诉Maven编译资源文件中找到的属性 - 添加一个实用程序文件
src / main / resources / RESOURCE_PATH
,其中只包含RESOURCE_PATH
/ li>
- 从Java中的实用程序文件中提取
RESOURCE_PATH
属性 - 重新生成项目例如,如果文件/文件夹结构更改)
POM
< < code>< project>
...
< properties>
< RESOURCE_PATH> $ {project.basedir} / src / main / resources< / RESOURCE_PATH>
< / properties>
...
< build>
...
< resources>
< resource>
< directory> $ {RESOURCE_PATH}< / directory>
< filtering> true< / filtering>
< / resource>
< / resources>
....
< / build>
...
< / project>
工具文件( src / main / resources / RESOURCE_PATH
)
$ {RESOURCE_PATH}
Java
/ **
*读取资源的相对路径目录从< code> RESOURCE_PATH< / code>文件位于
*< code> src / main / resources< / code>
* @返回相对路径到< code> resources< / code>在文件系统中,或
*< code> null< / code>如果有错误
* /
private static String getResourcePath(){
try {
URI resourcePathFile = System.class.getResource(/ RESOURCE_PATH)。toURI ;
String resourcePath = Files.readAllLines(Paths.get(resourcePathFile))。get(0);
URI rootURI = new File()。toURI();
URI resourceURI = new File(resourcePath).toURI();
URI relativeResourceURI = rootURI.relativize(resourceURI);
return relativeResourceURI.getPath();
} catch(Exception e){
return null;
}
}
public static void main(String [] args)throws URISyntaxException,IOException {
File file = new File(getResourcePath()+/ abc 。文本);
//在这里写文件
System.out.println(file.lastModified());
}
我希望工作。也许您需要 maven资源:resources plugin ,然后执行 mvn generate-resources process-resources
Here is the structure of my maven project:
main and test folders are under src folder, java and resources folders are under main folder, in the resources folder, there is a csv file ready for reading.
src
--main
|-- java
|-- resources
|-- test.csv
test
Currently, I am trying to overwrite the test.csv file using supercsv library.
I know that public CsvMapWriter(Writer writer, CsvPreference preference)
method is helpful on that.
So, I am trying to use OutputStreamWriter, but how to access that file like using:
InputStreamReader reader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("acacia_implexa.csv"));
mapReader = new CsvMapReader(reader, CsvPreference.STANDARD_PREFERENCE);
You can output text to a file in the resources
folder, but the process is a bit tedious.
- Tell Maven to write the location of your
resources
folder into a file - Read the location of your
resources
folder in Java - Use that location to write your file to
Notes
Why bother specifying the location of your
resources
folder file? Isn't itsrc/main/resources
always? Well no, if you have a nested module structure likerootModule ├─ childModule1 │ └─ src │ └─ main │ └─ resources └─ childModule2 └─ ...
then the relative path
src/main/resources
resolves toparentModule/src/main/resources
which does not exist.- Once you write the file to the
resources
folder, it will not be accessible throughClass.getResource()
.Class.getResource()
provides you with read access to files located in your classpath. The files located in yourresources
folder are "copied" to your binary output folder (read classpath) during "compilation". In order to access your newly created resource file with the help ofClass.getResource()
, you will have to recompile & restart the application. However, you can still access it by the location you used for writing the file.
Guide
- add
RESOURCE_PATH
property to the Maven pom, and tell Maven to compile properties found in resource files - add an utility file
src/main/resources/RESOURCE_PATH
, which only contains theRESOURCE_PATH
property - extract the
RESOURCE_PATH
property from the utility file in Java - rebuild the project if needed (e.g. if file/folder structure changes)
POM
<project>
...
<properties>
<RESOURCE_PATH>${project.basedir}/src/main/resources</RESOURCE_PATH>
</properties>
...
<build>
...
<resources>
<resource>
<directory>${RESOURCE_PATH}</directory>
<filtering>true</filtering>
</resource>
</resources>
....
</build>
...
</project>
Utility file (src/main/resources/RESOURCE_PATH
)
${RESOURCE_PATH}
Java
/**
* Reads the relative path to the resource directory from the <code>RESOURCE_PATH</code> file located in
* <code>src/main/resources</code>
* @return the relative path to the <code>resources</code> in the file system, or
* <code>null</code> if there was an error
*/
private static String getResourcePath() {
try {
URI resourcePathFile = System.class.getResource("/RESOURCE_PATH").toURI();
String resourcePath = Files.readAllLines(Paths.get(resourcePathFile)).get(0);
URI rootURI = new File("").toURI();
URI resourceURI = new File(resourcePath).toURI();
URI relativeResourceURI = rootURI.relativize(resourceURI);
return relativeResourceURI.getPath();
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) throws URISyntaxException, IOException {
File file = new File(getResourcePath() + "/abc.txt");
// write something to file here
System.out.println(file.lastModified());
}
I hope that works. Maybe you need maven resources:resources plugin and do mvn generate-resources process-resources
这篇关于如何将文本输出到资源文件夹Maven中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!