Y
yay_frogs
My problem: I need to create a buffer that needs a capacity sufficient
to hold a row of 80 characters that will be written out to a file, then
modified, then written out to a file again. In C, this could be done
like:
char linebuf[81];
/* code to insert chars in linebuf; as an example: */
linebuf[0] = 'H';
linebuf[1] = 'i';
linebuf[2] = 0; /* null char to terminate string */
/* write the line */
printf("%s\n", linebuf); // "Hi" is written out followed by a
newline.
/* modify line buffer again; no need to allocate/delete memory */
linebuf[0] = 'Y';
linebuf[1] = 'o';
linebuf[2] = '!';
linebuf[3] = 0;
/* write the line */
printf("%s\n", linebuf); // "Yo!" is written out followed by a
newline.
Now my problem is that in Java, I can't figure out how to do this
without allocating new objects. If a use a StringBuffer/StringBuilder
that has a length of 80 characters, there doesn't seem to be a way to
efficiently print only the number of characters that the current row
actually has. (The setLength method creates a new object.) If I use a
char[] in Java then the output routines ignore the terminating null
byte.
There has got to be a way to do this in Java! Thanks for any help.
to hold a row of 80 characters that will be written out to a file, then
modified, then written out to a file again. In C, this could be done
like:
char linebuf[81];
/* code to insert chars in linebuf; as an example: */
linebuf[0] = 'H';
linebuf[1] = 'i';
linebuf[2] = 0; /* null char to terminate string */
/* write the line */
printf("%s\n", linebuf); // "Hi" is written out followed by a
newline.
/* modify line buffer again; no need to allocate/delete memory */
linebuf[0] = 'Y';
linebuf[1] = 'o';
linebuf[2] = '!';
linebuf[3] = 0;
/* write the line */
printf("%s\n", linebuf); // "Yo!" is written out followed by a
newline.
Now my problem is that in Java, I can't figure out how to do this
without allocating new objects. If a use a StringBuffer/StringBuilder
that has a length of 80 characters, there doesn't seem to be a way to
efficiently print only the number of characters that the current row
actually has. (The setLength method creates a new object.) If I use a
char[] in Java then the output routines ignore the terminating null
byte.
There has got to be a way to do this in Java! Thanks for any help.