GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - CharArrayReader read(char[] b, int off, int len) method
Selected Reading
Java - CharArrayReader read(char[] b, int off, int len) methodDescriptionThe Java CharArrayReader read(char[] b, int off, int len) method reads characters into a specified portion of a character array. It attempts to read up to len characters and stores them in buf starting at index off. If fewer characters are available, it reads only those. If the end of the stream is reached, it returns -1. DeclarationFollowing is the declaration for java.io.CharArrayReader.read(char[] b, int off, int len) method − public int read(char[] b, int off, int len) Parameters
Return ValueThe actual number of characters read, -1 for the end of the stream. ExceptionIOException − If any I/O error occurs. Example - Using CharArrayReader read(char[] b, int off, int len) methodThe following example shows the usage of Java CharArrayReader read(char[] b, int off, int len) method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
CharArrayReader car = null;
char[] ch = {'H', 'E', 'L', 'L', 'O'};
char[] d = new char[5];
try {
// create new character array reader
car = new CharArrayReader(ch);
// read character to the destination buffer
car.read(d, 3, 2);
// for every character in the buffer
for (char c : d) {
int i = (int)c;
if(i == 0) {
System.out.println("0");
} else {
System.out.println(c);
}
}
} catch(IOException e) {
// if I/O error occurs
System.out.print("Stream is already closed");
} finally {
// releases any system resources associated with the stream
if(car!=null)
car.close();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − 0 0 0 H E Example - Reading in Chunks and Printing OutputThe following example shows the usage of Java CharArrayReader read(char[] b, int off, int len) method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "Hello, CharArrayReader!".toCharArray();
char[] buffer = new char[10]; // Buffer to store read characters
try (CharArrayReader reader = new CharArrayReader(data)) {
int charsRead;
System.out.println("Reading characters in chunks:");
while ((charsRead = reader.read(buffer, 0, buffer.length)) != -1) {
System.out.print(new String(buffer, 0, charsRead)); // Convert read characters to String
}
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Reading characters in chunks: Hello, CharArrayReader! Explanation
Example - Reading a Specific Portion of Data into an OffsetThe following example shows the usage of Java CharArrayReader read(char[] b, int off, int len) method. CharArrayReaderDemo.javapackage com.tutorialspoint;
import java.io.CharArrayReader;
import java.io.IOException;
public class CharArrayReaderDemo {
public static void main(String[] args) {
char[] data = "JavaProgrammingLanguage".toCharArray();
char[] buffer = new char[15]; // Buffer size is larger than read length
try (CharArrayReader reader = new CharArrayReader(data)) {
// Read 10 characters starting at index 5 in buffer
int charsRead = reader.read(buffer, 5, 10);
System.out.println("Buffer content after read operation:");
for (char c : buffer) {
System.out.print(c == '\0' ? '-' : c); // Print '-' for empty buffer spaces
}
} catch (IOException e) {
System.err.println("IOException occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Buffer content after read operation: -----JavaProgra Explanation
Key Points
java_io_chararrayreader.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |