Efficient writable character buffers in Java



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.

.



Relevant Pages

  • Javac-compilor error
    ... discipline id.e.programming Java. ... from standard input and writes to standard output, but it is possible to redirect the input ... error occurs while trying to open the file, an exception of type IllegalArgumentException ... then this number of characters, then extra spaces are added to the front of x to bring ...
    (Fedora)
  • Re: Newbie to java
    ... How do I do the same in Java? ... char c = b; ... Characters in Java uses Unicode, which means that in some cases you'll get ... as it's not a buffer of characters, ...
    (comp.lang.java.help)
  • help needed using ifstream::seekg with windows text file
    ... actually reads in the number of characters, ... to seek to any char length> 0, it doesn't seek to my next offset ... I know that windows and unix treat newlines differently. ... It behaves as if a newline is 1 char with sizeof 2 bytes. ...
    (comp.lang.cpp)
  • Re: gotchas.html: Missing Hex
    ... > There is a difference between entering characters into ... char chr = 'x'; (double quote for one type, ... I am not writing these in java program. ... have not read JLS yet. ...
    (comp.lang.java.help)
  • Re: gotchas.html: Missing Hex
    ... >> There is a difference between entering characters into ... existence of Java than the amount of words in the JLS and VMS (VM ... The problem with Java is that it is, in fact, a necessary restriction ... Java is a strongly typed language, ...
    (comp.lang.java.help)