Java : OutputStreamWriter - API使用例
OutputStreamWriter (Java SE 21 & JDK 21) の使い方まとめです。
だいたいのメソッドを網羅済みです。
API仕様書のおともにどうぞ。
概要
OutputStreamWriter クラスは、OutputStream から Writer へと橋渡しするためのクラスです。
OutputStreamWriter は使い終わったら close が必要です。
try-with-resources文 を使い、リソースリークが起きないようにしましょう。
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write("abcd");
}
System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]
final var os = new ByteArrayOutputStream();
try (final var writer = new BufferedWriter(new OutputStreamWriter(os))) {
writer.write("abcd");
writer.newLine();
writer.write("XYZ");
}
System.out.println(os);
// 結果
// ↓
//abcd
//XYZ
Writerで宣言されたフィールド
lock
「Java API 使用例 : Writer」をご参照ください。
コンストラクタ
OutputStreamWriter (OutputStream out)
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write("abcd");
}
System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]
OutputStreamWriter (OutputStream out, String charsetName)
final var sjis = "Shift_JIS";
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os, sjis)) {
writer.write("○△×");
}
System.out.println(os.toString(sjis)); // ○△×
final var bytes = os.toByteArray();
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, 126]
OutputStreamWriter (OutputStream out, Charset cs)
final var sjis = Charset.forName("Shift_JIS");
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os, sjis)) {
writer.write("○△×");
}
System.out.println(os.toString(sjis)); // ○△×
final var bytes = os.toByteArray();
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, 126]
OutputStreamWriter (OutputStream out, CharsetEncoder enc)
final var sjis = Charset.forName("Shift_JIS");
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os, sjis.newEncoder())) {
writer.write("○△×");
}
System.out.println(os.toString(sjis)); // ○△×
final var bytes = os.toByteArray();
System.out.println(Arrays.toString(bytes)); // [-127, -101, -127, -94, -127, 126]
メソッド
void close ()
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write("abcd");
}
System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]
// try-with-resources文を使わない例です。
final var os = new ByteArrayOutputStream();
final var writer = new OutputStreamWriter(os);
try {
writer.write("abcd");
} finally {
writer.close();
}
System.out.println(os); // abcd
System.out.println(Arrays.toString(os.toByteArray())); // [97, 98, 99, 100]
void flush ()
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write("abcd");
final var ret1 = os.toString();
System.out.println(ret1.isEmpty()); // true
writer.flush();
final var ret2 = os.toString();
System.out.println(ret2); // abcd
writer.write("XYZ");
final var ret3 = os.toString();
System.out.println(ret3); // abcd
}
final var ret4 = os.toString();
System.out.println(ret4); // abcdXYZ
String getEncoding ()
try (final var writer = new OutputStreamWriter(new ByteArrayOutputStream())) {
final var ret = writer.getEncoding();
System.out.println(ret); // UTF8
}
final var sjis = Charset.forName("Shift_JIS");
try (final var writer = new OutputStreamWriter(new ByteArrayOutputStream(), sjis)) {
final var ret = writer.getEncoding();
System.out.println(ret); // SJIS
}
void write (char[] cbuf, int off, int len)
final char[] cbuf = {'a', 'b', 'c', 'd'};
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 0, 1);
}
System.out.println(os); // a
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 0, 2);
}
System.out.println(os); // ab
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 0, 3);
}
System.out.println(os); // abc
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 0, 4);
}
System.out.println(os); // abcd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
//writer.write(cbuf, 0, 5); // IndexOutOfBoundsException
}
}
final char[] cbuf = {'a', 'b', 'c', 'd'};
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 0, 4);
}
System.out.println(os); // abcd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 1, 3);
}
System.out.println(os); // bcd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 2, 2);
}
System.out.println(os); // cd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(cbuf, 3, 1);
}
System.out.println(os); // d
}
void write (int c)
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write('a');
writer.write('b');
writer.write('c');
}
System.out.println(os); // abc
void write (String str, int off, int len)
final var str = "abcd";
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 0, 1);
}
System.out.println(os); // a
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 0, 2);
}
System.out.println(os); // ab
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 0, 3);
}
System.out.println(os); // abc
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 0, 4);
}
System.out.println(os); // abcd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
//writer.write(str, 0, 5); // IndexOutOfBoundsException
}
}
final var str = "abcd";
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 0, 4);
}
System.out.println(os); // abcd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 1, 3);
}
System.out.println(os); // bcd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 2, 2);
}
System.out.println(os); // cd
}
{
final var os = new ByteArrayOutputStream();
try (final var writer = new OutputStreamWriter(os)) {
writer.write(str, 3, 1);
}
System.out.println(os); // d
}
Writerで宣言されたメソッド
append, append, append, nullWriter, write, write
「Java API 使用例 : Writer」をご参照ください。