Mega Code Archive

 
Categories / Java / File Input Output
 

Read File in String Using Java BufferedInputStream Example

import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; public class Main {   public static void main(String[] args) throws Exception {     File file = new File("C:/ReadFile.txt");     FileInputStream fin = new FileInputStream(file);     BufferedInputStream bin = new BufferedInputStream(fin);     byte[] contents = new byte[1024];     int bytesRead = 0;     String strFileContents;     while ((bytesRead = bin.read(contents)) != -1) {       strFileContents = new String(contents, 0, bytesRead);       System.out.print(strFileContents);     }     bin.close();   } }