如何将文本输出到资源文件夹Maven中的文件 [英] How to output text to a file in resource folder Maven

查看:1010
本文介绍了如何将文本输出到资源文件夹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 文件夹中的文件,但过程有点单调乏味。


  1. 告诉Maven 将您的资源文件夹的位置写入文件

  2. 读取Java中资源文件夹的位置

  3. 使用该位置写入您的档案



注意




  • 为什么要指定资源文件夹文件的位置?是不是 src / main / resources 总是?如果你有一个嵌套的模块结构,如

      rootModule 
    ├─childModule1
    │└─ src
    │└─main
    │└─resources
    └─childModule2
    └─...

    那么相对路径 src / main / resources 解析为 parentModule / src / main / resources 不存在。


  • 将文件写入资源将不能通过 Class.getResource()访问。 Class.getResource()为您提供对位于类路径中的文件的读取访问。在编译期间,位于 resources 文件夹中的文件会复制到二进制输出文件夹(读类路径)。为了在 Class.getResource()的帮助下访问您新创建的资源文件,您必须重新编译&重新启动应用程序。



指南




  1. 向Maven pom添加 RESOURCE_PATH 属性,并告诉Maven编译资源文件中找到的属性

  2. 添加一个实用程序文件 src / main / resources / RESOURCE_PATH ,其中只包含 RESOURCE_PATH / li>
  3. 从Java中的实用程序文件中提取 RESOURCE_PATH 属性

  4. 重新生成项目例如,如果文件/文件夹结构更改)



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.

  1. Tell Maven to write the location of your resources folder into a file
  2. Read the location of your resources folder in Java
  3. Use that location to write your file to

Notes

  • Why bother specifying the location of your resources folder file? Isn't it src/main/resources always? Well no, if you have a nested module structure like

    rootModule   
    ├─ childModule1   
    │  └─ src    
    │     └─ main    
    │        └─ resources    
    └─ childModule2 
       └─ ...  
    

    then the relative path src/main/resources resolves to parentModule/src/main/resources which does not exist.

  • Once you write the file to the resources folder, it will not be accessible through Class.getResource(). Class.getResource() provides you with read access to files located in your classpath. The files located in your resources folder are "copied" to your binary output folder (read classpath) during "compilation". In order to access your newly created resource file with the help of Class.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

  1. add RESOURCE_PATH property to the Maven pom, and tell Maven to compile properties found in resource files
  2. add an utility file src/main/resources/RESOURCE_PATH, which only contains the RESOURCE_PATH property
  3. extract the RESOURCE_PATH property from the utility file in Java
  4. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆