Java ClassLoader Load getContentFromFile(String fileName, Object context)

Here you can find the source of getContentFromFile(String fileName, Object context)

Description

get Content From File

License

Apache License

Declaration

public static String getContentFromFile(String fileName, Object context) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class Main {
    public static String getContentFromFile(String fileName, Object context) throws Exception {
        File file = getFileFromPath(context, fileName);
        FileInputStream fin = new FileInputStream(file);
        String response = convertStreamToString(fin);
        fin.close();// w  ww . j ava 2  s.c  om
        return response;
    }

    private static File getFileFromPath(Object obj, String fileName) {
        ClassLoader classLoader = obj.getClass().getClassLoader();
        URL resource = classLoader.getResource(fileName);
        return new File(resource.getPath());
    }

    public static String convertStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        reader.close();
        return sb.toString();
    }
}

Related

  1. getAllArquillianLibs()
  2. getAsProperties(final String name)
  3. getBaseDir()
  4. getBytes(File file)
  5. getConfigFileInputStream(String configFilePath)
  6. getDirectories(String path)
  7. getFile(final String filename)
  8. getFile(final String name)
  9. getFile(Object obj, String filename)