How to Read a File Input Stream Into a String

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer'south Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

How to convert InputStream object to a String in Java?


Coffee provides I/O Streams to read and write data where, a Stream represents an input source or an output destination which could be a file, i/o devise, other plan etc.

At that place are ii types of streams available −

  • InputStream − This is used to read (sequential) data from a source.
  • OutputStream − This is used to write data to a destination.

FileInputStream

This class reads the data from a specific file (byte by byte). It is ordinarily used to read the contents of a file with raw bytes, such every bit images.

Converting an InputStream object to String

You tin can convert an InputStream Object int to a String in several ways using core Java. You tin can as well employ external libraries like IOUtils, Guava for this purpose. Following are some ways to convert an InputStream object to String in Coffee (not including external libraries).

Using BufferedReader

The readLine() method of the BufferedReader form reads a unmarried line from the contents of the current reader. To catechumen an InputStream Object int to a String using this method.

  • Instantiate an InputStreamReader grade by passing your InputStream object equally parameter.
  • Then, create a BufferedReader, by passing in a higher place obtained InputStreamReader object as a parameter.
  • Now, read each line from this reader using the readLine() method and suspend it to a StringBuffer object.
  • Finally convert the StringBuffer to Cord using the toString() method.

Example

import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class InputStreamToString{    public static void main(Cord args[]) throws IOException {       //Creating an InputStream object       InputStream inputStream = new FileInputStream("D:/sample.txt");       //creating an InputStreamReader object       InputStreamReader isReader = new InputStreamReader(inputStream);       //Creating a BufferedReader object       BufferedReader reader = new BufferedReader(isReader);       StringBuffer sb = new StringBuffer();       String str;       while((str = reader.readLine())!= cypher){          sb.append(str);       }       Organization.out.println(sb.toString());    } }

Output

Tutorials Bespeak originated from the thought that there exists a class of readers who respond better to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.

Using the Scanner class

The nextLine() method of the Scanner class reads the contents of the underlying inputStream line by line. To convert an InputStream Object int to a String using this method.

  • Instantiate the Scanner class past passing your InputStream object equally parameter.
  • Read each line from this Scanner using the nextLine() method and suspend information technology to a StringBuffer object.
  • Finally convert the StringBuffer to String using the toString() method.

Example

import coffee.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Scanner; public grade InputStreamToString {    public static void main(String args[]) throws IOException {       //Creating an InputStream object       InputStream inputStream = new FileInputStream("D:/sample.txt");       //Creating a Scanner object       Scanner sc = new Scanner(inputStream);       //Reading line past line from scanner to StringBuffer       StringBuffer sb = new StringBuffer();       while(sc.hasNext()){          sb.append(sc.nextLine());       }       Organization.out.println(sb.toString());    } }

Output

Tutorials Point originated from the idea that there exists a class of readers who answer amend to on-line content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.

Using the InputStreamReader class

The read() method of the InputStreamReaderclass accepts a character array as a parameter and reads the contents of the electric current Stream to the given array. To convert an InputStream Object int to a String using this method.

  • Instantiate an InputStreamReader form by passing your InputStream object as parameter.
  • Read the contents of the current stream reader to a graphic symbol array using the read() method of the InputStreamReader class.
  • Finally convert the character to a String by passing it as a parameter to its constructor.

Example

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public form FileInputStreamExample {    public static void main(String args[]) throws IOException {       //Creating a File object       File file = new File("D:/sample.txt");       //Creating an InputStream object       InputStream inputStream = new FileInputStream(file);       //creating an InputStreamReader object       InputStreamReader isReader = new InputStreamReader(inputStream);       //Creating a character array       char charArray[] = new char[(int) file.length()];       //Reading the contents of the reader       isReader.read(charArray);       //Converting character array to a Cord       String contents = new String(charArray);       System.out.println(contents);    } }

Output

Tutorials Betoken originated from the idea that there exists a class of readers who reply better to on-line  content and prefer to acquire new skills at their own step from the comforts of their drawing rooms.

raja

Published on 01-Aug-2019 thirteen:32:forty

  • Related Questions & Answers
  • How to catechumen a Cord to an InputStream object in Java?
  • Coffee Program to Catechumen InputStream to Cord
  • catechumen InputStream to ByteArray in java
  • How to convert InputStream to byte array in Java?
  • Catechumen Java String Object to Boolean Object
  • How to convert a cord to JavaScript object?
  • how to convert Object assortment to Cord array in java
  • Java Plan to convert Java String to Float Object
  • How to convert a Engagement object to LocalDate object in coffee?
  • How to convert a string to a Python class object?
  • How to convert a JavaScript appointment object to a cord?
  • How tin can we convert a JSON string to a JSON object in Java?
  • How to convert a string in to appointment object in JavaScript?
  • Convert string of time to fourth dimension object in Coffee
  • Convert a string to hierarchical object - JavaScript

saineponsibleaut.blogspot.com

Source: https://www.tutorialspoint.com/how-to-convert-inputstream-object-to-a-string-in-java

0 Response to "How to Read a File Input Stream Into a String"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel