I have a method which reads a file and returns a string, but I wanted to return a reader. I want to convert the string to a reader, or I want to read the file and return the reader. How can I do this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use
java.io.StringReader:return new StringReader(string);.Next time you need a reader, you can check the “Direct known subclasses” of the
Readerclass. Same goes forInputStream, etc. The place to start is the javadoc – it contains quite a lot of useful information.But for your task at hand, you’d better follow Jon Lin’ advice of simply using a
FileReader. There is no need to go throughString. (For that, my advice from the previous paragraph applies as well)