Java Resource Path Get getResourcePath()

Here you can find the source of getResourcePath()

Description

Reads the relative path to the resource directory from the <code>testResourcePath</code> file located in <code>src/test/resources</code> http://stackoverflow.com/questions/21567497/how-to-output-text-to-a-file-in-resource-folder-maven

License

Open Source License

Return

the relative path to the resources in the file system, or null if there was an error

Declaration

public static String getResourcePath() 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.URI;

public class Main {
    /**/*from w  ww  .jav  a  2 s. c  o  m*/
     * Reads the relative path to the resource directory from the <code>testResourcePath</code> file located in
     * <code>src/test/resources</code>
     * http://stackoverflow.com/questions/21567497/how-to-output-text-to-a-file-in-resource-folder-maven
     * @return the relative path to the <code>resources</code> in the file system, or
     *         <code>null</code> if there was an error
     */
    public static String getResourcePath() {
        try {
            String resourcePathFile = System.class.getResource("/testResourcePath").getFile();
            String resourcePath = new BufferedReader(new FileReader(resourcePathFile)).readLine();
            URI rootURI = new File("").toURI();
            URI resourceURI = new File(resourcePath).toURI();
            URI relativeResourceURI = rootURI.relativize(resourceURI);
            return relativeResourceURI.getPath();
        } catch (Exception e) {
            return null;
        }
    }
}

Related

  1. getResourceFileRelativeToBase(final File baseDir, final String resourcePath)
  2. getResourceListing(Class clazz, String path)
  3. getResourceListing(Class clazz, String path)
  4. getResourceListing(Class clazz, String path)
  5. getResourceListing(Class clazz, String path, String glob)
  6. getResourcePath(Class clazz, String fileName)
  7. getResourcePath(final Class bundleClazz, final String pathToFile)
  8. getResourcePath(Object object, String resource)
  9. getResourcePath(String filename)