Java InputStreamReader Read readFile(Class cls, String filename)

Here you can find the source of readFile(Class cls, String filename)

Description

read File

License

Open Source License

Declaration

public static String readFile(Class<?> cls, String filename)
            throws IOException 

Method Source Code

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String readFile(Class<?> cls, String filename)
            throws IOException {
        InputStream stream = cls.getResourceAsStream(filename);
        if (stream != null) {
            StringBuffer fileData = new StringBuffer();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(stream));
            char[] buf = new char[1024];
            for (int bufLen = reader.read(buf); bufLen != -1; bufLen = reader
                    .read(buf)) {/* w ww .  ja v a 2 s  .co  m*/
                String readData = String.valueOf(buf, 0, bufLen);
                fileData.append(readData);
                buf = new char[1024];
            }
            reader.close();
            return fileData.toString();
        }
        throw new IOException("file cannot be found: " + filename);
    }
}

Related

  1. load(File file, String encoding)
  2. load(final File file, final Class clazz)
  3. load(String fileName)
  4. loadAsText(InputStream in, String encoding, int bufferSize)
  5. readFile(Class cl, String filename)
  6. readFile(ClassLoader classloader, String filename)
  7. readFile(File f)
  8. readFile(File f)
  9. readFile(File f)