Java : InputStreamReader con ejemplos

InputStreamReader (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de InputStreamReader.

Nota :


Summary

Un InputStreamReader es un puente entre los flujos de bytes y los flujos de caracteres: lee bytes y los decodifica en caracteres utilizando un conjunto de caracteres especificado. El conjunto de caracteres que utiliza puede especificarse por nombre o puede proporcionarse explícitamente, o puede utilizarse el conjunto de caracteres predeterminado. (Traducción automática)

Class diagram

final byte[] bytes = "abc".getBytes();

try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 97 : a

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 98 : b

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 99 : c

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}
final byte[] bytes = """
        abcd
        XYZ
        12345
        """.getBytes();

try (final var reader = new BufferedReader(new InputStreamReader(
        new ByteArrayInputStream(bytes)))) {
    System.out.println(reader.readLine()); // abcd
    System.out.println(reader.readLine()); // XYZ
    System.out.println(reader.readLine()); // 12345
    System.out.println(reader.readLine()); // null
}

Fields declared in Reader

lock

Consulte el siguiente enlace.

Constructors

InputStreamReader (InputStream in)

Crea un InputStreamReader que utiliza el conjunto de caracteres predeterminado. (Traducción automática)

final byte[] bytes = "abc".getBytes();

try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 97 : a

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 98 : b

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 99 : c

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}

InputStreamReader (InputStream in, String charsetName)

Crea un InputStreamReader que utiliza el conjunto de caracteres nombrado. (Traducción automática)

final var sjis = "Shift_JIS";
final byte[] bytes = "○△×".getBytes(sjis);

try (final var reader = new InputStreamReader(
        new ByteArrayInputStream(bytes), sjis)) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 9675 : ○

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 9651 : △

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 215 : ×

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}

InputStreamReader (InputStream in, Charset cs)

Crea un InputStreamReader que utiliza el conjunto de caracteres dado. (Traducción automática)

final var sjis = Charset.forName("Shift_JIS");
final byte[] bytes = "○△×".getBytes(sjis);

try (final var reader = new InputStreamReader(
        new ByteArrayInputStream(bytes), sjis)) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 9675 : ○

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 9651 : △

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 215 : ×

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}

InputStreamReader (InputStream in, CharsetDecoder dec)

Crea un InputStreamReader que utiliza el decodificador de conjuntos de caracteres indicado. (Traducción automática)

final var sjis = Charset.forName("Shift_JIS");
final byte[] bytes = "○△×".getBytes(sjis);

try (final var reader = new InputStreamReader(
        new ByteArrayInputStream(bytes), sjis.newDecoder())) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 9675 : ○

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 9651 : △

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 215 : ×

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}

Methods

void close ()

Cierra la transmisión y libera todos los recursos del sistema asociados con ella. (Traducción automática)

final byte[] bytes = "abcd".getBytes();
try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    final var cbuf = new char[4];

    System.out.println(reader.read(cbuf)); // 4
    System.out.println(Arrays.toString(cbuf)); // [a, b, c, d]
}
// An example without a try-with-resources statement.
final byte[] bytes = "abcd".getBytes();
final var reader = new InputStreamReader(new ByteArrayInputStream(bytes));
try {
    final var cbuf = new char[4];

    System.out.println(reader.read(cbuf)); // 4
    System.out.println(Arrays.toString(cbuf)); // [a, b, c, d]
} finally {
    reader.close();
}

String getEncoding ()

Devuelve el nombre de la codificación de caracteres que utiliza esta secuencia. (Traducción automática)

final byte[] bytes = "abcd".getBytes();

try (final var reader = new InputStreamReader(
        new ByteArrayInputStream(bytes))) {
    final var ret = reader.getEncoding();
    System.out.println(ret); // UTF8
}
final var sjis = Charset.forName("Shift_JIS");
final byte[] bytes = "abcd".getBytes(sjis);

try (final var reader = new InputStreamReader(
        new ByteArrayInputStream(bytes), sjis)) {
    final var ret = reader.getEncoding();
    System.out.println(ret); // SJIS
}

int read ()

Lee un solo caracter. (Traducción automática)

final byte[] bytes = "abc".getBytes();

try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    final var ret1 = reader.read();
    System.out.printf("%d : %c%n", ret1, ret1); // 97 : a

    final var ret2 = reader.read();
    System.out.printf("%d : %c%n", ret2, ret2); // 98 : b

    final var ret3 = reader.read();
    System.out.printf("%d : %c%n", ret3, ret3); // 99 : c

    final var ret4 = reader.read();
    System.out.println(ret4); // -1
}

int read (char[] cbuf, int off, int len)

Lee caracteres en una parte de una matriz. (Traducción automática)

final byte[] bytes = "abcdefg".getBytes();

try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [a,  ,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 2)); // 2
    System.out.println(Arrays.toString(cbuf)); // [b, c,  ,  ,  ]

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [d, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 4)); // 1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]

    System.out.println(reader.read(cbuf, 0, 5)); // -1
    System.out.println(Arrays.toString(cbuf)); // [g, e, f,  ,  ]
}
final byte[] bytes = "abcde".getBytes();

try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    final var cbuf = new char[5];

    System.out.println(reader.read(cbuf, 4, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  ,  , a]

    System.out.println(reader.read(cbuf, 3, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  ,  , b, a]

    System.out.println(reader.read(cbuf, 2, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ ,  , c, b, a]

    System.out.println(reader.read(cbuf, 1, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [ , d, c, b, a]

    System.out.println(reader.read(cbuf, 0, 1)); // 1
    System.out.println(Arrays.toString(cbuf)); // [e, d, c, b, a]
}
final byte[] bytes = "abcdefg".getBytes();

try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    final var cbuf = new char[3];

    System.out.println(reader.read(cbuf, 0, 3)); // 3
    System.out.println(Arrays.toString(cbuf)); // [a, b, c]

    //reader.read(cbuf, 0, 4); // IndexOutOfBoundsException
    //reader.read(cbuf, 3, 1); // IndexOutOfBoundsException
}

boolean ready ()

Indica si esta secuencia está lista para ser leída. (Traducción automática)

final byte[] bytes = "abc".getBytes();

try (final var reader = new InputStreamReader(new ByteArrayInputStream(bytes))) {
    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // b

    System.out.println(reader.ready()); // true
    System.out.printf("%c%n", reader.read()); // c

    System.out.println(reader.ready()); // false
    System.out.println(reader.read()); // -1
}

Methods declared in Reader

mark, markSupported, nullReader, read, read, reset, skip, transferTo

Consulte el siguiente enlace.


Related posts

To top of page