Java : BufferedReader con ejemplos

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

Nota :


Summary

Lee texto desde un flujo de entrada de caracteres, almacenando en búfer los caracteres para permitir la lectura eficiente de caracteres, matrices y líneas. (Traducción automática)

Class diagram

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

Files.writeString(path, """
        abcd
        XYZ
        12345
        """);

try (final var reader = Files.newBufferedReader(path)) {

    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

BufferedReader (Reader in)

Crea un flujo de entrada de caracteres con almacenamiento en búfer que utiliza un búfer de entrada de tamaño predeterminado. (Traducción automática)

final var s = """
        abcd
        XYZ
        """;

try (final var reader = new BufferedReader(new StringReader(s))) {

    System.out.println(reader.readLine()); // abcd
    System.out.println(reader.readLine()); // XYZ
    System.out.println(reader.readLine()); // null
}

BufferedReader (Reader in, int sz)

Crea un flujo de entrada de caracteres de almacenamiento en búfer que utiliza un búfer de entrada del tamaño especificado. (Traducción automática)

final var s = """
        abcd
        XYZ
        """;

try (final var reader = new BufferedReader(new StringReader(s), 128)) {

    System.out.println(reader.readLine()); // abcd
    System.out.println(reader.readLine()); // XYZ
    System.out.println(reader.readLine()); // null
}

Methods

void close ()

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

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

Files.writeString(path, "abcd");

try (final var reader = Files.newBufferedReader(path)) {

    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 var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

Files.writeString(path, "abcd");

final var reader = Files.newBufferedReader(path);
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();
}

Stream<String> lines ()

Devuelve un Stream, cuyos elementos son líneas leídas desde este BufferedReader. (Traducción automática)

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

Files.writeString(path, """
        abcd
        XYZ
        12345
        """);

try (final var reader = Files.newBufferedReader(path)) {

    final var lines = reader.lines();
    lines.forEach(System.out::println);
}

// Result
// ↓
//abcd
//XYZ
//12345

void mark (int readAheadLimit)

Marca la posición actual en la secuencia. (Traducción automática)

try (final var reader = new BufferedReader(new StringReader("abcde"))) {

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

    reader.mark(3);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();
    reader.mark(2);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    try {
        reader.reset();
    } catch (IOException e) {
        System.out.println(e);
    }

    // Result
    // ↓
    //java.io.IOException: Mark invalid
}

boolean markSupported ()

Indica si esta secuencia admite la operación mark(), lo cual hace. (Traducción automática)

try (final var reader = Files.newBufferedReader(
        Path.of("R:", "java-work", "aaa.txt"))) {
    System.out.println(reader.markSupported()); // true
}

try (final var reader = new BufferedReader(new StringReader("abcd"))) {
    System.out.println(reader.markSupported()); // true
}

int read ()

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

try (final var reader = new BufferedReader(new StringReader("abc"))) {

    System.out.printf("%c%n", reader.read()); // a
    System.out.printf("%c%n", reader.read()); // b
    System.out.printf("%c%n", reader.read()); // c
    System.out.println(reader.read()); // -1
}

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

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

try (final var reader = new BufferedReader(new StringReader("abcdefg"))) {

    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,  ,  ]
}
try (final var reader = new BufferedReader(new StringReader("abcde"))) {

    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]
}
try (final var reader = new BufferedReader(new StringReader("abcdefg"))) {

    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
}

String readLine ()

Lee una línea de texto. (Traducción automática)

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

Files.writeString(path, """
        abcd
        XYZ
        12345
        """);

try (final var reader = Files.newBufferedReader(path)) {

    System.out.println(reader.readLine()); // abcd
    System.out.println(reader.readLine()); // XYZ
    System.out.println(reader.readLine()); // 12345
    System.out.println(reader.readLine()); // null
}

boolean ready ()

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

final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt

Files.writeString(path, "abc");

try (final var reader = Files.newBufferedReader(path)) {

    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
}

void reset ()

Restablece la transmisión a la marca más reciente. (Traducción automática)

try (final var reader = new BufferedReader(new StringReader("abcde"))) {

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

    reader.mark(3);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    reader.reset();
    reader.mark(2);

    System.out.printf("%c%n", reader.read()); // d
    System.out.printf("%c%n", reader.read()); // e
    System.out.println(reader.read()); // -1

    try {
        reader.reset();
    } catch (IOException e) {
        System.out.println(e);
    }

    // Result
    // ↓
    //java.io.IOException: Mark invalid
}

Methods declared in Reader

nullReader, read, read, skip, transferTo

Consulte el siguiente enlace.


Related posts

To top of page