Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - String to StringReader

Tags:

java

I have 3rd party library function which only takes Reader as parameter. And I have String that I need to pass into function. How do I do that ?

String s = "Some String";
StringReader sReader = new StringReader(s);

// 3rd Party Function Definition is public File saveResult(Reader source);

Can I do as File aFile = saveResult(sReader); ? Or Should I create buffer and call read() first ?

I tried both way, and I am keep getting null pointer exception.

like image 774
Arjun Patel Avatar asked Dec 03 '25 02:12

Arjun Patel


1 Answers

Hope this should be enough to serve your purpose:

String string = "Some String";
StringReader stringReader = new StringReader(string);
File fileName = saveResult(stringReader); // calling Third party library method
like image 55
1218985 Avatar answered Dec 05 '25 17:12

1218985



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!