Here you can find the source of readFile(InputStream inputStream)
Parameter | Description |
---|---|
inputStream | the inputStream of the file to be read |
Parameter | Description |
---|---|
IOException | if an error occurs while reading |
public static String readFile(InputStream inputStream) throws IOException
//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; } }