Sunday, 6 March 2022

Java: Get the resource file path

Below snippet return the resource file path.

public static String resourceFilePath(String resourceFile) {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceFile);
		return url.getPath();
}

 

Find the below class.

 

FileUtil.java

package com.sample.app.util;

import java.net.URL;

public class FileUtil {

	public static String resourceFilePath(String resourceFile) {
		ClassLoader classLoader = FileUtil.class.getClassLoader();
		URL url = classLoader.getResource(resourceFile);
		return url.getPath();
	}

}

 

 

 

 

You may like

File programs in Java

Create file parent directories if they are not exist

Get file name from absolute path

Count number of lines in a file

Download file from Internet using Java

Get the content of resource file as string

No comments:

Post a Comment