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 to write contents of InputStream to an OutputStream in Java

1 Answer

0 votes
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
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 inputStream = new ByteArrayInputStream(str.getBytes());
        
        OutputStream outputStream = new FileOutputStream("data.txt");

        byte[] bytearray = new byte[str.length()];
        while ((inputStream.read(bytearray)) > 0) {
            outputStream.write(bytearray, 0, str.length());
        }

        String out =  new String(bytearray, StandardCharsets.UTF_8);
        System.out.println(out);
        
        inputStream.close();
        outputStream.close();
    }
}
 
  
  
  
/*
run:
  
Java object-oriented programming language
  
*/

 



answered Oct 11, 2023 by avibootz

Related questions

3 answers 187 views
3 answers 194 views
2 answers 120 views
1 answer 155 views
1 answer 131 views
1 answer 123 views
...