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 - StringReader class
Selected Reading
Java - StringReader classIntroductionThe Java StringReader class is a character stream whose source is a string. Class declarationFollowing is the declaration for Java.io.StringReader class − public class StringReader extends Reader FieldFollowing are the fields for Java.io.StringReader class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Properly closing a StringReaderThe following example shows the usage of StringReader close() method. StringReaderDemo.javapackage com.tutorialspoint;
import java.io.StringReader;
import java.io.IOException;
public class StringReaderDemo {
public static void main(String[] args) throws IOException {
StringReader reader = new StringReader("Hello, world!");
int data = reader.read();
System.out.println("First character: " + (char) data);
reader.close(); // closing the reader
System.out.println("Reader closed.");
}
}
OutputLet us compile and run the above program, this will produce the following result− First character: H Reader closed. Explanation
Example - Basic use of mark() and reset()The following example shows the usage of StringReader mark(int readAheadLimit) method. StringReaderDemo.javapackage com.tutorialspoint;
import java.io.StringReader;
public class StringReaderDemo {
public static void main(String[] args) throws Exception {
StringReader reader = new StringReader("abcdef");
System.out.print((char) reader.read()); // a
reader.mark(3); // mark at position after 'a'
System.out.print((char) reader.read()); // b
System.out.print((char) reader.read()); // c
reader.reset(); // go back to mark (before 'b')
System.out.print((char) reader.read()); // b
System.out.print((char) reader.read()); // c
reader.close();
}
}
OutputLet us compile and run the above program, this will produce the following result− abcbc Explanation
Example - Checking if mark() is supportedThe following example shows the usage of StringReader markSupported() method. StringReaderDemo.javapackage com.tutorialspoint;
import java.io.StringReader;
public class StringReaderDemo {
public static void main(String[] args) {
StringReader reader = new StringReader("Hello, world!");
System.out.println("Mark supported? " + reader.markSupported());
reader.close();
}
}
OutputLet us compile and run the above program, this will produce the following result− Mark supported? true Explanation
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. |