I have an application that is creating a flat file for a legacy application. Something that is common when creating flat files is having to "pad" empty fields with either zeros or spaces. Today, the way that problem was solved is to have all classes create a string literal for this padded space which is ugly and hard to read and verify the length of a given field.
I want to replace this with a common function like the following:
public String pad(int len, char c) {
StringBuffer sb = new StringBuffer();
for (int i=0; i<len; i++) {
sb.append(c);
}
return sb.toString();
}
However I'm concerned about performance as this function would be used fairly prevalently within the codebase. My mind then gravitated towards wrapping this function in a cache (multi-key hashmap, aka hashtable) but I feel like there has to be a more standard way of doing something like this.
What is the least silly way to accomplish this goal?