Java 如何从字符串创建 BufferedReader?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39702017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:21:45 来源:igfitidea点击:
How can I create a BufferedReader from a String?
提问by water
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
?
我正在尝试将一个字符串传递给我的BufferedReader
. 如何将“测试”传递String
给读者而不是来自 的输入System.in
?
String test = "test";
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
采纳答案by SarthAk
You can modify your code as below
您可以修改您的代码如下
String test = "test";
Reader inputString = new StringReader(test);
BufferedReader reader = new BufferedReader(inputString);
回答by Alexey Romanov
No point in buffering a string. Just
缓冲字符串没有意义。只是
String aString = ...;
Reader inFromUser = new StringReader(aString);