Java Resource Get getResourceContent(Object object, String resource)

Here you can find the source of getResourceContent(Object object, String resource)

Description

Returns the name of the resource file in the form of a string

License

Open Source License

Parameter

Parameter Description
resource a parameter

Declaration

public static String getResourceContent(Object object, String resource) 

Method Source Code


//package com.java2s;
/*//from ww w .ja  v a2s . c o m
       
This file is part of the Xapagy Cognitive Architecture 
Copyright (C) 2008-2017 Ladislau Boloni
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.
    
You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
*/

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class Main {
    /**
     * Returns the name of the resource file in the form of a string
     * 
     * @param resource
     * @return
     */
    public static String getResourceContent(Object object, String resource) {
        URL testURL = object.getClass().getResource(resource);
        String testFile = null;
        try {
            testFile = new URI(testURL.toString()).getPath();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            // should not normally happen
        }
        File file = new File(testFile);
        if (!file.exists()) {
            throw new Error("File: " + testFile + " does not exist");
        }
        return testFile;
    }
}

Related

  1. getResourceAsStream(String resourceLocation)
  2. getResourceAsString(String prefix, String resource)
  3. getResourceAsString(String resource)
  4. getResourceBase()
  5. getResourceBundleLocaleString(String sKey, Object[] sParam, String sResourceBundle)
  6. getResourceDir(final Class testClass)
  7. getResourceExpression(String select)
  8. getResourceFile(Class clazz, String fileName)
  9. getResourceFile(String name)