Here you can find the source of getResourcePath(String fileName)
Parameter | Description |
---|---|
IOException | if a valid path to an existing file cannot be returned |
private static String getResourcePath(String fileName) throws IOException
//package com.java2s; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; public class Main { /**/*from w w w . j av a 2 s. c om*/ * Return the absolute path of the specified file resource on the classpath. * @throws IOException if a valid path to an existing file cannot be returned */ private static String getResourcePath(String fileName) throws IOException { URL url = Thread.currentThread().getContextClassLoader().getResource(fileName); if (url == null) { throw new IOException("Failed to locate resource " + fileName); } File file; try { file = new File(url.toURI()); } catch (URISyntaxException urise) { throw new IOException("Invalid URL: " + urise); } String path = file.getAbsolutePath(); if (!file.exists()) { throw new IOException("File " + path + " does not exist"); } return path; } }