私のコードは、0 から 255 までの整数値を取り、それを文字列としてファイルに書き込む必要があります。非常に高速に繰り返し呼び出される可能性があるため、高速である必要があるため、負荷が高い場合に最適化が顕著になります。ここには、大量のデータをファイルに書き込む効率的な方法を扱う他の質問がありますが、少量のデータはどうですか?
これが私の現在のアプローチです:
public static void writeInt(final String filename, final int value)
{
try
{
// Convert the int to a string representation in a byte array
final String string = Integer.toString(value);
final byte[] bytes = new byte[string.length()];
for (int i = 0; i < string.length(); i++)
{
bytes[i] = (byte)string.charAt(i);
}
// Now write the byte array to file
final FileOutputStream fileOutputStream = new FileOutputStream(filename);
fileOutputStream.write(bytes, 0, bytes.length);
fileOutputStream.close();
}
catch (IOException exception)
{
// Error handling here
}
}
ここでは a は役に立たないと思いBufferedOutputStream
ます: バッファのフラッシュを構築するオーバーヘッドは、おそらく 3 文字の書き込みには逆効果ですよね? 他に改善できる点はありますか?