简体   繁体   中英

How can I create a BufferedReader from a String?

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));

You can modify your code as below

String test = "test";
Reader inputString = new StringReader(test);
BufferedReader reader = new BufferedReader(inputString);

No point in buffering a string. Just

String aString = ...;
Reader inFromUser = new StringReader(aString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM