get String From Resource File - Java java.io

Java examples for java.io:InputStreamReader

Description

get String From Resource File

Demo Code


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.regex.Pattern;

public class Main{
    public static void main(String[] argv){
        String fileName = "java2s.com";
        System.out.println(getStringFromResourceFile(fileName));
    }//from  w  w  w  . j  a  v  a2 s .  com
    public static String getStringFromResourceFile(String fileName) {
        try {
            InputStream is = StringHelper.class
                    .getResourceAsStream(fileName);
            return read(new InputStreamReader(is));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static String read(InputStreamReader isr) throws IOException {
        StringBuffer buffer = new StringBuffer();
        Reader in = new BufferedReader(isr);
        int ch;
        while ((ch = in.read()) > -1) {
            buffer.append((char) ch);
        }
        in.close();
        return buffer.toString();
    }
}

Related Tutorials