Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,943 questions

51,883 answers

573 users

How to convert InputStream to String in Java

2 Answers

0 votes
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.io.IOException;
 
public class MyClass {
    public static void main(String args[]) throws IOException {
        String s = "Java object-oriented programming language";
        InputStream istream = new ByteArrayInputStream(s.getBytes());
        BufferedReader br = new BufferedReader(new InputStreamReader(istream));
         
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
         
        br.close();
        
        String istreamtext = sb.toString();
 
        System.out.println(istreamtext);
    }
}
 
 
 
  
/*
run:
     
Java object-oriented programming language
   
*/

 



answered Jan 16, 2022 by avibootz
edited Nov 12, 2023 by avibootz
0 votes
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

 
public class MyClass {
    public static void main(String args[]) throws IOException {
        String str = "Java object-oriented programming language";
        
        InputStream istream = new ByteArrayInputStream(str.getBytes());
        
        String istreamtext = new BufferedReader(
                            new InputStreamReader(istream, StandardCharsets.UTF_8))
                            .lines()
                            .collect(Collectors.joining("\n"));

        System.out.println(istreamtext);
    }
}
 
 
 
  
/*
run:
     
Java object-oriented programming language
   
*/

 



answered Nov 12, 2023 by avibootz

Related questions

3 answers 188 views
3 answers 195 views
1 answer 131 views
1 answer 91 views
1 answer 155 views
...