How to output text to a file in resource folder Maven

前端 未结 2 700
生来不讨喜
生来不讨喜 2020-12-18 12:07

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,

相关标签:
2条回答
  • 2020-12-18 12:34

    You can override the file but that would only work in IDE, if it would be executed as part of jar it would fail because it is not a file any more( its file inside file)

    Better to externalize file and read/overwrite from that location

    If you still would like to read this file and overwrite it then you can do something like

    Thread.getclassloader().get resource("/");
    

    This will give you path up to your target/classes directory you can navigate to resources from here using double dots

    0 讨论(0)
  • 2020-12-18 12:53

    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

    0 讨论(0)
提交回复
热议问题