ほとんどの場合、スペースで構成されているが特定の既知の位置にはまばらに配置された固定長レコードを作成する必要があります。本質的に私が作り出す必要があるのは、レコードが多数の固定長フィールドで構成されている従来のファイル形式です。使用を検討固定長lenfth文字列またはcharバッファの特定の位置に書き込む方法
StringBuilder b = new StringBuilder();// Or use StringBufer if you need synchronization
b.append("----------"); //use dash instead of space for visibility
int pos = 4;
String replacement = "foo";
b.replace(pos, pos + replacement.length(), replacement); //Attention: if the length of the replacement is greater than the length of the original content, the exceeding chars will be appended
System.out.println(b); //----foo---
ありがとうございます - 私は、StringBuilderのドキュメントを見てみましたが、置き換えメソッドが欠けていました。私は、文字列の長さのチェックを追加して、誤ってバッファを展開していないことを確認しました。 if(string.length()> length)throw new new IllegalArgumentException( "入力文字列の長さ>フィールドの長さ"); – Shane