2 ways to Read text from file in String using Apache commons IOUtils and BufferedInputStream (and FileInputStream) in java file IO | or Convert inputStream to String



We will read c:/myFile.txt

c:/myFile.txt looks like this >
You are learning File IO from javaMadeSoEasy.com



Program 1 to Convert inputStream to String using Apache commons IOUtils in java>


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import org.apache.commons.io.IOUtils;
/** JavaMadeSoEasy.com */
public class ReadFileInString {
   public static void main(String... args) throws IOException {
          FileInputStream fis = null;
          BufferedInputStream bis = null;
          fis = new FileInputStream("c:/myFile.txt");
          bis = new BufferedInputStream(fis);
          System.out.println("Reading from text file using "
                       + "IOUtils.copy(inputStream, stringWriter, 'encoding')");
          StringWriter stringWriter = new StringWriter();
          IOUtils.copy(bis, stringWriter, "UTF-8");
          String textReadFromFileInString = stringWriter.toString();
          System.out.println("textReadFromFileInString = "
                       + textReadFromFileInString);
          IOUtils.closeQuietly(bis); // closing inputStream
   }
}
/*OUTPUT
Reading from text file using IOUtils.copy(inputStream, stringWriter, 'encoding')
textReadFromFileInString = You are learning File IO from javaMadeSoEasy.com
*/

Program 2 to Read text from file in String using BufferedInputStream (and FileInputStream) in java file IO >


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class ReadFileInString {
   public static void main(String... args) {
          FileInputStream fis = null;
          BufferedInputStream bis = null;
         
          try {
                 fis = new FileInputStream("c:/myFile.txt");
                 bis= new BufferedInputStream(fis);
                
                 System.out.println("Reading from text file in char array");
                
                 //char array will store all characters of file
                 char[] charReadFromFile = new char[1000]; //change it according to file size
                 int index=0;
                
                 int ch;
                 while ((ch = bis.read()) != -1) { //read till end of file
                       charReadFromFile[index++] = (char) ch;
                 }
                
                 //Convert char array to String
                 System.out.println("Convert char array to String");
                 String textReadFromFileInString=new String(charReadFromFile);
                 System.out.println("textReadFromFileInString = "+textReadFromFileInString);
                
          } catch (IOException e) {
                 e.printStackTrace();
          } finally {
                 try {
                       if (bis != null)
                              bis.close(); //close BufferedInputStream
//it will close FileInputStream

                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
   }
}
/*OUTPUT
Reading from text file in char array
Convert char array to String
textReadFromFileInString = You are learning File IO from javaMadeSoEasy.com
*/




RELATED LINKS>


Read text from file using FileInputStream in java file IO



Read text from file using BufferedInputStream (and FileInputStream) in java file IO



Write String to file using FileOutputStream in java file IO


eEdit
Must read for you :