concatenation : Java Glossary

go to home page C words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2009-09-24 Roedy Green, Canadian Mind Products
concatenation
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.
// simple concatenation
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

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:

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:
Rem glue a.txt, b.txt and c.txt into a new file d.txt 
Rem leaving a.txt, b.txt and c.txt as they were.
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 you cannot prepend like this:

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:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/concatenation.html J:\mindprod\jgloss\concatenation.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.100]
You are visitor number 1.