Reads data from a text file defined in resources to a String - Android android.content.res

Android examples for android.content.res:Resources

Description

Reads data from a text file defined in resources to a String

Demo Code

import android.content.res.Resources;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main{

    /**/*from   w w w .  j  a  v  a2  s  .  c o  m*/
     * Reads data from a text file defined in resources to a {@link String}.
     * 
     * @param resources
     *            the resources database to use
     * @param resourceId
     *            the file identifier to convert to a {@link String}
     * @return a {@link String} containing the read data, or <tt>null</tt> if an
     *         error was encountered
     */
    public static String rawResourceToString(final Resources resources,
            final int resourceId) {
        final InputStream inputStream = resources
                .openRawResource(resourceId);
        final InputStreamReader inputStreamReader = new InputStreamReader(
                inputStream);
        final BufferedReader bufferedReader = new BufferedReader(
                inputStreamReader);

        String nextLine;
        final StringBuilder body = new StringBuilder();

        try {
            while ((nextLine = bufferedReader.readLine()) != null) {
                body.append(nextLine);
                body.append('\n');
            }
        } catch (IOException e) {
            return null;
        }

        return body.toString();
    }

}

Related Tutorials