Java : OutputStreamWriter con ejemplos
OutputStreamWriter (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de OutputStreamWriter.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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);
// Result
// ↓
//abcd
//XYZ
Fields declared in Writer
Constructors
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]
Methods
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]
// An example without a try-with-resources statement.
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
}
Methods declared in Writer
Related posts
- Ejemplos de API