Concatenation means gluing Strings end to end to form one big String. You specify it
in Java with the +
operator (the same as used for addition), e. g.
final String greeting = "Happy" + " " + "birthday.";
Javac does concatenation of String literals, such as in the example above, at
compile time, so they have no run-time overhead.
Javac implements concatenation with calls to StringBuffer in JDK (Java Development Kit) 1.4- and StringBuilder in 1.5+. These are not as efficient as using StringBuilder directly since there is no way to specify the size of
the buffer.
Alternate Concatenation Techniques
- StringBuffer
- StringBuilder
- StringWriter
- CharArrayWriter
- FastCat
- LazyString. Don’t concatenate
appended Strings, just save individual appended
Strings up in an ArrayList,
and procrastinate the concatenation to the last minute, when you know the exact
size of the buffer you need to compose the String.
- Use a StringWriter implemented as an ArrayList<char[]> of
8096 char buffers. Allocate
an additional buffer if needed to handle each append.
Appended Strings may sometimes have to span buffers.
Buffers are completely filled before starting another. To convert to a String at the end, allocate a char[]
buffer the exact size and copy over each of the 8096-char buffers. Ideally convert to a
String without yet another copy.
Concatenating Files Programmatically
You can concatenate files
programmatically by reading the pieces and writing them out in one big file. There a
few things to be aware of:
- As a legacy from the pre-DOS CPM (Cost Per thousand/Mille impressions)
days, some text files have an EOL (End Of Line)
character as the last character. These will have to be removed when you
concatenate.
- If your text file don’t end with a \r\n or
\n etc, you will have to insert one before the next
chunk.
- If your target file is the same as one of your component files, you must copy
the output to a temporary file, delete the target, then rename the temporary to the
target, or use logic that produces the same effect.
- If you concatenation merely tacks files onto the end of the target, you can
avoid copying the target itself. The problem with this sort of logic is if the
process fails part way through, the original target file is corrupted. The
advantage is that it is quicker — which might be important for a very long
file.
- For any sort of copy where the target file already exists, it is wise to copy
to a temporary file then do a delete and rename at the last second. This way you
don’t corrupt the target file if the copy fails part way through.
Concatenating Files at the Command Prompt
You can
concatenate files at the command prompt (or my execing a command processor with copy
commands.) To glue three files together to form d.txt you
would type:
copy a.txt + b.txt + c.txt d.txt
copy /A warns copy that the files might have
EOL
characters.
copy /B tells copy to treat any EOL
characters like ordinary data.
Take Command does not support these options. It presumes text files do not use
obsolete EOL
characters.
It is safe to append onto your target like this:
However,
Learning More
Oracle’s Javadoc on
StringBuffer class : available:
Oracle’s Javadoc on
StringBuilder class : available:
Oracle’s Javadoc on
StringWriter class : available:
Oracle’s Javadoc on
CharArrayWriter class : available: