Concantenation 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 1.4- and StringBuildder 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 days, some text files have an EOL 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
Sun’s Javadoc on
StringBuffer class : available:
Sun’s Javadoc on
StringBuilder class : available:
Sun’s Javadoc on
StringWriter class : available:
Sun’s Javadoc on
CharArrayWriter class : available: