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 100637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:37:04+00:00 2026-05-11T00:37:04+00:00

I am a little confused here. I would like to do something like this:

  • 0

I am a little confused here.

I would like to do something like this:

  1. create some kind of buffer I can write into
  2. clear the buffer
  3. use a printf()-like function several times to append a bunch of stuff into the buffer based on some complicated calculations I only want to do once
  4. use the contents of the buffer and print it to several PrintStream objects
  5. repeat steps 2-4 as necessary

e.g.:

SuperBuffer sb = new SuperBuffer();   /* SuperBuffer is not a real class, so I don't know what to use here */ PrintStream[] streams = new PrintStream[N]; /* ... initialize this array to several streams ... */  while (!done) {     sb.clear();     sb.printf('something %d something %d something %d',          value1, value2, value3);     if (some_complicated_condition())         sb.printf('something else %d something else %d', value4, value5);     /* ... more printfs to sb ... */     for (PrintStream ps : streams)         ps.println(sb.getBuffer()); } 

It looks like wrapping a PrintWriter around StringWriter will do what I want for the sb object above, except there’s no clear() method. I suppose I could create a new PrintWriter and StringWriter object each time through the loop, but that seems like a pain. (in my real code I do this in several places, not just once in one loop…)

I’ve also used java.nio.CharBuffer and other NIO buffers a lot, and that seems like a promising approach, but I’m not sure how I can wrap them with an object that will give me printf() functionality.

any advice?

  • 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. 2026-05-11T00:37:04+00:00Added an answer on May 11, 2026 at 12:37 am

    ah: I think I’ve got it. The Formatter class has a format() method that’s like printf(), and it can be constructed to wrap around any kind of object that implements Appendable. CharBuffer implements Appendable, and I can clear() or read out the contents of the CharBuffer as necessary.

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

Sidebar

Related Questions

I'm a little confused by some PHP syntax I've come across. Here is an
I am a little confused by the multitude of ways in which you can
I am getting a little confused and need some help please. Take these two
I want to post some data via javascript to another domain. Something like: http://www.othersite.com/submitfunnyname?name=blah
I'm a little confused about how the standard library will behave now that Python
I am a little confused as to the definition of classes as Models or
Im a little confused over how to use the .NET Trace and Debug classes.
When supplying dates to a stored procedure via a parameter I'm a little confused
I'm playing around with Project Euler's Problem 220 , and I'm a little confused
I'm doing some ASP.NET development in VS and have just found an interesting little

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.