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 :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
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
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
Constructors
FilterReader (Reader in)
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 ()
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)
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 ()
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 ()
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)
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 ()
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 ()
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)
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
Related posts
- Ejemplos de API