- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert/read an input stream into a string in java?
Java provides FileInputStream class for reading data from a file. This class provides read() method to read contents of a file character by character. Create a StringBuffer object and append character by character to it using the append() method. Finally, convert the contents of StringBuffer object to String using toString() method.
Example
import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class StreamToString { public static void main(String args[]) throws IOException { File file = new File("da"); FileInputStream fis = new FileInputStream(file); int size=fis.available(); StringBuffer buffer = new StringBuffer(); for(int i = 0; i< size; i++) { buffer.append((char)fis.read()); } System.out.println(buffer.toString()); } }
Output
Hi how are you
- Related Articles
- How to convert an input stream to byte array in java?
- How to convert File into a Stream in Java?
- How to convert string Stream to join them in Java?
- How to Convert a Java 8 Stream to an Array?
- How to convert a comma separated String into an ArrayList in Java?
- Convert an Iterator to Stream in Java
- Convert an Iterable to Stream in Java
- How to convert a String into int in Java?
- Java Program to read the next byte of data from the input stream
- How to read an input image and print it into an array in matplotlib?
- Program to convert Stream to an Array in Java
- How to read the contents of a webpage into a string in java?
- How to convert Stream to TreeSet in Java?
- How to convert an array into JavaScript string?
- How to read a file into a string in Golang?

Advertisements