How to read a file to string in Java

To read a file to string in Java you can use the following snippet.

Sample Java

public static string readFileToString(FileInputStream _fileInputStream)
{
	try {  
		DataInputStream dataInputStream = new DataInputStream (_fileInputStream);  
		byte[] Bytes = new byte[dataInputStream.available ()];  
		dataInputStream.readFully (Bytes);  
		dataInputStream.close ();  
		String result = new String (Bytes, 0, Bytes.length, "Cp850");  
		return result;  
	}  
	catch (Exception e) {  
		e.printStackTrace();  
		return "";
	} 
}

Please Share us:
Please rate this snippet

5 thoughts on “How to read a file to string in Java

Leave a Reply