Java : FilterReader con ejemplos

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

Nota :


Summary

Clase abstracta para leer secuencias de caracteres filtrados. La clase abstracta FilterReader proporciona métodos predeterminados que pasan todas las solicitudes a la secuencia contenida. Las subclases de FilterReader deberían anular algunos de estos métodos y también pueden proporcionar métodos y campos adicionales. (Traducción automática)

Class diagram

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(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
}

Fields

protected Reader in

El flujo de entrada de caracteres subyacente. (Traducción automática)

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }

    Reader getIn() {
        return in;
    }
}

final var sr = new StringReader("abc");
try (final var reader = new MyReader(sr)) {
    System.out.println(reader.getIn() == sr); // true
}

Fields declared in Reader

lock

Consulte el siguiente enlace.

Constructors

FilterReader (Reader in)

Crea un nuevo lector filtrado. (Traducción automática)

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(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
}

Methods

void close ()

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

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(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
}
// An example without a try-with-resources statement.
class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

final var reader = new MyReader(new StringReader("abc"));
try {
    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
} finally {
    reader.close();
}

void mark (int readAheadLimit)

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

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(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("IOException! : " + e.getMessage());
    }

    // Result
    // ↓
    //IOException! : Mark invalid
}

boolean markSupported ()

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

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

{
    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 = new MyReader(Files.newBufferedReader(path))) {
        System.out.println(reader.markSupported()); // true
    }
}
{
    try (final var reader = new MyReader(new StringReader("abcd"))) {
        System.out.println(reader.markSupported()); // true
    }
}
{
    final var url = URI.create("https://example.com/").toURL();
    try (final var reader = new MyReader(new InputStreamReader(url.openStream()))) {
        System.out.println(reader.markSupported()); // false
    }
}

int read ()

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

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(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)

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(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 MyReader(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 MyReader(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
}

boolean ready ()

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

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

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 = new MyReader(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. (Traducción automática)

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(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("IOException! : " + e.getMessage());
    }

    // Result
    // ↓
    //IOException! : Mark invalid
}

long skip (long n)

Salta caracteres. (Traducción automática)

class MyReader extends FilterReader {
    MyReader(Reader in) {
        super(in);
    }
}

try (final var reader = new MyReader(new StringReader("abcdefghij"))) {
    System.out.printf("%c%n", reader.read()); // a

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

    System.out.println(reader.skip(2)); // 2
    System.out.printf("%c%n", reader.read()); // f

    System.out.println(reader.skip(3)); // 3
    System.out.printf("%c%n", reader.read()); // j

    System.out.println(reader.skip(1)); // 0
    System.out.println(reader.read()); // -1
}

try (final var reader = new MyReader(new StringReader("abcde"))) {
    System.out.printf("%c%n", reader.read()); // a

    System.out.println(reader.skip(10)); // 4
    System.out.println(reader.read()); // -1
}

Methods declared in Reader

nullReader, read, read, transferTo

Consulte el siguiente enlace.


Related posts

To top of page