Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:32:48+00:00 2026-06-04T22:32:48+00:00

How can I create class that takes an implentation of OutputStream and writes the

  • 0

How can I create class that takes an implentation of OutputStream and writes the the content to it?

For example, the following print method is wrong and will not compile. What are the options or better techniques?

public class FooBarPrinter{
    private OutputStream o;
    public FooBarPrinter(OutputStream o){
        this.o=o;
    }

    public void print(String s){        
        o.write(s);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:32:49+00:00Added an answer on June 4, 2026 at 10:32 pm

    A generic OutputStream doesn’t have the ability to write a String directly to it. Instead, you need to get the byte[] of the String and then write them out to the stream.

    public void print(String s){ 
        o.write(s.getBytes());
    }
    

    Refer to the OutputStream java documentation for more information on the supported write data types.

    My suggestion for making this better is to make sure the OutputStream you provide in the constructor is a BufferedOutputStream, as this will improve the performance of your code. It works by buffering some of the data in memory, and writing it out to disk in big chunks instead of many smaller writes.

    Here is the java documentation for BufferedOutputStream

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a class that can instantiate arrays at runtime by giving
I'm trying to figure out the best way to create a class that can
Suppose I am designing a class that can handle any database technology to create
I want to create a C++ class with the following type: It can be
I was testing a String multiplier class with a multiply() method that takes 2
I want to create a generic class that takes a type parameter and restrict
I would like to create an abstract class with an abstract method which can
I have a class that I can have many instances of. Inside it creates
How can I create a singleton class in Objective C?
If I create a PolyModel class, such as Person, can I create an Expando

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.