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 a String to an InputStream in Java

3 Answers

0 votes
import java.io.InputStream;
import java.io.ByteArrayInputStream;

public class MyClass {
    public static void main(String args[]) throws Exception {
        String s = "Java programming language";
        
        InputStream input = new ByteArrayInputStream(s.getBytes("UTF-8")); 
         
        int i;
        while ((i = input.read()) > -1) {
            System.out.print((char) i);
        }
    }
}
 
 
 
/*
run:
 
Java programming language
 
*/

 



answered Jun 29, 2020 by avibootz
edited May 19, 2024 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 c c++ c# python";
         
        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 c c++ c# python
    
*/

 



answered May 19, 2024 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 c c++ c# python go";
         
        InputStream istream = new ByteArrayInputStream(str.getBytes("UTF-8"));
         
        String istreamtext = new BufferedReader(
                            new InputStreamReader(istream, StandardCharsets.UTF_8))
                            .lines()
                            .collect(Collectors.joining("\n"));
 
        System.out.println(istreamtext);
    }
}
  
  
  
   
/*
run:
 
Java c c++ c# python go
 
*/

 



answered May 19, 2024 by avibootz

Related questions

3 answers 195 views
2 answers 120 views
1 answer 131 views
1 answer 91 views
1 answer 155 views
1 answer 66 views
1 answer 90 views
...