Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a BufferedReader from a String?

Tags:

java

I am trying to pass a String to my BufferedReader. How can I pass "test" as String to the reader rather than the input from System.in ?

String test = "test"; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
like image 857
water Avatar asked Sep 26 '16 11:09

water


People also ask

Which method is for reading String in BufferedReader class?

This class provides a method known as readLine() which reads and returns the next line from the source and returns it in String format. The BufferedReader class doesn't provide any direct method to read an integer from the user you need to rely on the readLine() method to read integers too.

How do I get BufferedReader from InputStream?

The BufferedReader can't read the InputStream directly; So, we need to use an adapter like InputStreamReader to convert bytes to characters format. For example: // BufferedReader -> InputStreamReader -> InputStream BufferedReader br = new BufferedReader( new InputStreamReader(inputStream, StandardCharsets. UTF_8));


1 Answers

You can modify your code as below

String test = "test"; Reader inputString = new StringReader(test); BufferedReader reader = new BufferedReader(inputString); 
like image 106
SarthAk Avatar answered Sep 23 '22 17:09

SarthAk