Java : Reader with Examples
Reader (Java SE 21 & JDK 21) with Examples.
You will find code examples on most Reader methods.
Summary
final var path = Path.of("R:", "java-work", "aaa.txt");
System.out.println(path); // R:\java-work\aaa.txt
Files.writeString(path, "abcXYZ");
try (final Reader reader = Files.newBufferedReader(path)) {
System.out.printf("%c%n", reader.read()); // a
System.out.printf("%c%n", reader.read()); // b
System.out.printf("%c%n", reader.read()); // c
final var cbuf = new char[3];
System.out.println(reader.read(cbuf)); // 3
System.out.println(Arrays.toString(cbuf)); // [X, Y, Z]
}
Fields
protected Object lock
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Constructors
Reader ()
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Reader (Object lock)
protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.
Methods
abstract void close ()
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 Reader 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 Reader 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();
}
void mark (int readAheadLimit)
try (final Reader 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("IOException! : " + e.getMessage());
}
// Result
// ↓
//IOException! : Mark invalid
}
boolean markSupported ()
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 Reader reader = Files.newBufferedReader(path)) {
System.out.println(reader.markSupported()); // true
}
try (final Reader reader = new StringReader("abcd")) {
System.out.println(reader.markSupported()); // true
}
final var url = URI.create("https://example.com/").toURL();
try (final Reader reader = new InputStreamReader(url.openStream())) {
System.out.println(reader.markSupported()); // false
}
static Reader nullReader ()
try (final var reader = Reader.nullReader()) {
System.out.println(reader.read()); // -1
System.out.println(reader.ready()); // false
}
int read ()
try (final Reader reader = 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)
try (final Reader reader = new StringReader("abcde")) {
final var cbuf1 = new char[3];
System.out.println(reader.read(cbuf1)); // 3
System.out.println(Arrays.toString(cbuf1)); // [a, b, c]
final var cbuf2 = new char[5];
System.out.println(reader.read(cbuf2)); // 2
System.out.println(Arrays.toString(cbuf2)); // [d, e, , , ]
final var cbuf3 = new char[2];
System.out.println(reader.read(cbuf3)); // -1
System.out.println(Arrays.toString(cbuf3)); // [ , ]
}
abstract int read (char[] cbuf, int off, int len)
try (final Reader reader = 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 Reader reader = 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 Reader reader = 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
}
int read (CharBuffer target)
try (final Reader reader = new StringReader("abc")) {
final var target = CharBuffer.allocate(5);
System.out.println(reader.read(target)); // 3
System.out.println(Arrays.toString(target.array())); // [a, b, c, , ]
}
boolean ready ()
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 Reader 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 ()
try (final Reader 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("IOException! : " + e.getMessage());
}
// Result
// ↓
//IOException! : Mark invalid
}
long skip (long n)
try (final Reader reader = 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 Reader reader = new StringReader("abcde")) {
System.out.printf("%c%n", reader.read()); // a
System.out.println(reader.skip(10)); // 4
System.out.println(reader.read()); // -1
}
long transferTo (Writer out)
try (final Reader reader = new StringReader("abcde")) {
try (final var writer = new StringWriter()) {
System.out.println(reader.transferTo(writer)); // 5
System.out.println(writer); // abcde
}
}
Related posts
- API Examples