Java InputStreamReader Read readFile(InputStream inputStream)

Here you can find the source of readFile(InputStream inputStream)

Description

Read a text file

License

Open Source License

Parameter

Parameter Description
inputStream the inputStream of the file to be read

Exception

Parameter Description
IOException if an error occurs while reading

Return

the text from the file as a String

Declaration

public static String readFile(InputStream inputStream) throws IOException 

Method Source Code


//package com.java2s;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

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

public class Main {
    /**/*w w  w.  j  a v  a 2  s.  c om*/
     * Read a text file
     * 
     * @param fileURL
     *            the filename (full path) of the file to be read
     * @return the text from the file as a String
     * @throws IOException
     *             if an error occurs while reading
     */
    public static String readFile(String fileURL) throws IOException {

        InputStream is = null;

        is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileURL);

        if (is == null) {
            is = new FileInputStream(new File(fileURL));
        }

        BufferedReader fr = new BufferedReader(new InputStreamReader(is));

        String text = null;

        StringBuffer sb = new StringBuffer("");

        try {

            int bufsize = 1024;
            char[] buf = new char[bufsize];
            buf[0] = '\0';

            int len = -1;
            int off = 0;
            while ((len = fr.read(buf, off, bufsize)) != -1) {
                sb.append(buf, off, len);
                buf[0] = '\0';
            }

            text = sb.toString();

        } finally {
            if (fr != null) {
                fr.close();
            }
        }
        return text;
    }

    /**
     * Read a text file
     * 
     * @param inputStream
     *            the inputStream of the file to be read
     * @return the text from the file as a String
     * @throws IOException
     *             if an error occurs while reading
     */
    public static String readFile(InputStream inputStream) throws IOException {

        BufferedReader fr = new BufferedReader(new InputStreamReader(inputStream));

        String text = null;

        StringBuffer sb = new StringBuffer("");
        try {

            int bufsize = 1024;
            char[] buf = new char[bufsize];
            buf[0] = '\0';

            int len = -1;
            int off = 0;
            while ((len = fr.read(buf, off, bufsize)) != -1) {
                sb.append(buf, off, len);
                buf[0] = '\0';
            }

            text = sb.toString();

        } finally {
            if (fr != null) {
                fr.close();
            }
        }
        return text;
    }
}

Related

  1. readFile(IFile theFile)
  2. readFile(InputStream aStream)
  3. readFile(InputStream in)
  4. readFile(InputStream in)
  5. readFile(InputStream inputStream)
  6. readFile(InputStream inputStream)
  7. readFile(InputStream is)
  8. readFile(InputStream location)
  9. readFile(InputStream stream, StringBuilder builder)