esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

How do I convert a String to a Reader?
Java provides a StringReader class expressly for this purpose. java.io.StringReader has a constructor that takes a String argument.


 
import java.io.StringReader;
...
String myString = "Testing 1 2 3...";
StringReader myStringReader = new StringReader(myString);

// Then, use standard Reader-reading techniques
// to access the Reader
int len = -1; // Number of chars read
char [] buf = new char[256]; // Characters read from Reader
try {
while ((len = myStringReader.read(buf, 0, 256)) != -1) {
System.out.println("Next chunk from Reader is " + (new String(buf, 0, len)));
}
} catch (IOException ioe) {
System.err.println("Error reading from Reader :" + e.getMessage());
}




Further Information
Author of answer: themanwiththenose

Comments
Comments to this answer are only viewable by members. Login or become a member!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.