Java InputStreamReader Read readFileFromResource(String filename)

Here you can find the source of readFileFromResource(String filename)

Description

read File From Resource

License

Open Source License

Parameter

Parameter Description
filename xx.text

Declaration

public static String readFileFromResource(String filename) 

Method Source Code


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

import java.io.BufferedReader;

import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    /**/*ww  w.  j  a v  a 2 s . c  o m*/
     * 
     * @param filename   xx.text
     * @return
     */
    public static String readFileFromResource(String filename) {
        try {
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                stringBuilder.append(line);
            }
            br.close();
            return stringBuilder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. readFileData(File file)
  2. readFileData(String directoryLocation, String fileName)
  3. readFileFromClassPath(String file)
  4. readFileFromDisk(String filepath)
  5. readFileFromResource(@SuppressWarnings("rawtypes") Class refClass, String filePath)
  6. readFileIntoLines(File file)
  7. readFileIntoLinesOfLongs(File file)
  8. readFileLineByLine(final File filePath)
  9. readFileList(File file)