To create a StringReader
in Java, you can use the following code:
StringReader reader = new StringReader("Hello, World!");Sruoce:www.lautturi.com
This creates a new StringReader
called reader
that reads from the string "Hello, World!". The StringReader
is a class that allows you to read a string as if it were a stream of characters.
You can then use the read
method of the StringReader
to read the characters of the string, like this:
int c = reader.read(); // reads the first character of the string
The read
method returns the ASCII value of the character that it reads. To convert the value to a char
, you can use a type cast, like this:
char ch = (char) reader.read(); // reads the first character of the string and converts it to a char
It is important to remember to close the StringReader
when you are finished reading from it, to release any resources that it is using. You can do this using the close
method, like this:
reader.close();