Wednesday, May 25, 2016

Convert InputStream to String in Java

Sometimes in Java world it is necessary to convert an InputStream to a String for further processing. There are several ways to do it, but here is a fantastic trick I recently discovered in this article:
final String text = new Scanner(inputStream)
        .useDelimiter("\\A").next();
The idea behind this trick is regex \A. It matches the beginning of input. Since there is only one beginning of input the whole contents of the stream is returned as the first (and the last) token.

There is a problem with the code above. It would throw an exception if the provided stream is empty. However this is easy to fix:
final Scanner scanner = new Scanner(inputStream)
        .useDelimiter("\\A");
final String text = scanner.hasNext() ? scanner.next() : "";
If a specific charset must be used it could be passed as the second argument to the Scanner constructor:
final Scanner scanner = new Scanner(inputStream, "ISO-8859-1")
        .useDelimiter("\\A");
final String text = scanner.hasNext() ? scanner.next() : "";
That's all there is to it.
Thanks for reading!

78 comments:

  1. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    hadoop training in bangalore

    ReplyDelete
  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    R Programming Training in Chennai

    ReplyDelete
  3. I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.
    AWS Training in Bangalore

    ReplyDelete
  4. I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You.
    ME/M.Tech Project Center in Chennai | ME/M.Tech Project Center in Velachery

    ReplyDelete
  5. Awesome post. Really you are shared very informative concept.. Thank you for sharing. Keep on updating..
    Best Microsoft Azure Training Institute in Chennai | Best Microsoft Azure Training Institute in Velachery

    ReplyDelete
  6. I have to voice my passion for your kindness giving support to those people that should have guidance on this important matter.
    safety professional course in chennai

    ReplyDelete
  7. The information you shared is very effective for learners.

    Visit for a real time training for Java Training in Chennai | Best Java Training Institute in Chennai | Java Master Program Training in Chennai

    ReplyDelete
  8. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here
    fire and safety course in chennai

    ReplyDelete
  9. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.


    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  10. Extraordinary blog. you put Good stuff. All the themes were clarified briefly.Thanks for sharing that post.Keep in blogging…
    No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | Web Designing Training Institute in Chennai

    ReplyDelete
  11. Nice and informative article. Thanks for sharing such nice article, keep on updating.
    Graphic Design Training Institute in Chennai | Graphic Design Training Institute in Velachery

    ReplyDelete
  12. This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating
    Certified Ethical Hacking Training in Chennai | Hacking Training in Saidapet

    ReplyDelete
  13. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it.
    Cisco CCNP Certification Exam Center in Chennai | Best CCNP Training in Alandur

    ReplyDelete
  14. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    No.1 Microsoft Azure Training Institute in Chennai | No.1 Microsoft Azure Training Institute in Velachery

    ReplyDelete
  15. Your blog is really amazing with smart and cute content.keep updating such an excellent article
    CLOUD COMPUTING Classes in Chennai | CLOUD COMPUTING Courses in Velachery

    ReplyDelete
  16. Thanks for sharing wonderful post..What a innovative post..keep update post regularly..
    Selenium Testing Course in Chennai | Selenium Testing Course in Velachery

    ReplyDelete
  17. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it…Best Dotnet Training Institute in Chennai|Best Dotnet Training Institute in Velachery

    ReplyDelete
  18. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    No.1 Blue Prism Training Institute in Chennai | No.1 Blue Prism Training Institute in Velachery | No.1 Blue Prism Training Institute in Kanchipuram

    ReplyDelete
  19. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Automation Anywhere Training with Placement in Chennai | Automation Anywhere Training with Placement in Tambaram

    ReplyDelete
  20. What a fantastic information from your blog...Always keep sharing..
    PHP Project Center in Chennai | PHP Project Center in Velachery

    ReplyDelete
  21. My rather long internet look up has at the end of the day been compensated with pleasant insight to talk about with my family and friends.
    SEO Training Institute in Chennai | SEO Training Institute in Velachery

    ReplyDelete
  22. Hi, I am really happy to find such a helpful and fascinating post that is written in well manner. Thanks for sharing such an informative post.
    JAVA and J2EE Training Institute in Chennai | JAVA and J2EE Training Institute in Velachery

    ReplyDelete
  23. Best R Programming Training in Bangalore offered by myTectra. India's No.1 R Programming Training Institute. Classroom, Online and Corporate training in R Programming
    r programming training

    ReplyDelete
  24. Nice Post! It is really interesting to read from the beginning & I would like to share your blog to my circles, keep your blog as updated.
    No.1 Automation Anywhere Training Institute in Chennai | No.1 Automation Anywhere Training Institute in Velachery

    ReplyDelete
  25. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    No.1 Blue Prism Training Institute in Chennai | No.1 Blue Prism Training Institute in Kanchipuram

    ReplyDelete
  26. I think things like this are really interesting. I absolutely love to find unique places like this. It really looks super creepy though!! Best R Programming Training in Chennai

    ReplyDelete
  27. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Python Certification Course in Chennai | Python Certification Course in OMR

    ReplyDelete
  28. Thank you so much for sharing this worth able content with us. Keep blogging article like this.
    Best Cloud Computing Training Institute in Chennai |Best Cloud Computing Training Institute in Taramani

    ReplyDelete
  29. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Blue Prism Robotic Process Automation in Chennai | Blue Prism Robotic Process Automation in Chennai

    ReplyDelete
  30. Nice Blog… Wonderful Information and really very much useful. Thanks for sharing and keep updating…
    Python Training Institute in Chennai | Python Training Institute in Velachery

    ReplyDelete
  31. I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details.
    Python Exam Centers in Chennai | Python Exam Centers in Chennai

    ReplyDelete
  32. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Hardware and Networking Training in Chennai | Hardware and Networking Training in Taramani

    ReplyDelete
  33. This comment has been removed by the author.

    ReplyDelete
  34. This comment has been removed by the author.

    ReplyDelete
  35. Great blog..it was Really informative and useful..Thanks for sharing..keep update big data Hadoop training and certification in Chennai

    ReplyDelete
  36. This blog very easily understandable. Thanks for sharing such an informative post with us.This is a nice post in an interesting line of content.
    C and C++ Training in Chennai | C and C++ Training in Taramani

    ReplyDelete
  37. Really it was an awesome blog...... Very interesting to read, .It’s very helpful for me, Big thanks for the useful info and keep updating…
    CCNP Training Institute in Chennai | CCNP Training Institute in Velachery

    ReplyDelete
  38. Your Blog is really awesome with useful and helpful content for us.Thanks for sharing ..keep updating more information.
    Oracle Training in Chennai | Oracle Training in Kanchipuram

    ReplyDelete
  39. Your Blog is really amazing with useful information you are shared here...Thank you for sharing such an informative and awesome post with us. Keep blogging article like this……..
    AWS Training Institute in Chennai | AWS Training Institute in Velachery

    ReplyDelete
  40. Thank you for taking the time to provide us with your valuable information. Keep sharing your post regularly..
    Blueprism Exam Center in Chennai | Blueprism Exam Center in Velachery

    ReplyDelete
  41. Really I enjoy this blog….. Very nice post… Thanks for sharing and keep updating
    AWS Training Institute in Chennai | AWS Training Institute in Velachery

    ReplyDelete
  42. Really it was an awesome blog...... Very interesting to read, .It’s very helpful for me, Big thanks for the useful info and keep updating…
    Best UIPath Training Institute in Chennai | Best UIPath Training Institute in Velachery

    ReplyDelete
  43. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.
    Read more : R Programming Course Fees | R Language training in Chennai

    ReplyDelete
  44. Thank you so much for sharing such an amazing post with useful information with us. Keep updating such a wonderful blog….
    Best CCNA Training Institute in Chennai| CCNA Training Center in Velachery

    ReplyDelete
  45. Thank you for taking the time to provide us with your valuable information.keep sharing your post regularly..
    Ethical Hacking Training Course in Chennai | Ethical Hacking Training Course in Nanganallur

    ReplyDelete
  46. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Best Dotnet Training Institute in Chennai |Best Dotnet Training Institute in Kanchipuram

    ReplyDelete
  47. Nice blog…. with lovely information. Really very useful article for us thanks for sharing such a wonderful blog. Keep updating…..
    Best Linux Training Institute in Pallikaranai| Best Linux Training Institute in Velachery

    ReplyDelete
  48. It's really a nice experience to read your post. Thank you for sharing this useful information. If you are looking for more about hadoop training in chennai velachery | hadoop training course fees in chennai | Hadoop Training in Chennai Omr

    ReplyDelete
  49. Awesome article…. Really very helpful information for us. Keep it up. Keep blogging. Looking to reading your next post,. Thank You for Sharing This Information
    Best PCB Training Institute in Taramani| Best PCB Training Institute in Velachery

    ReplyDelete
  50. Nice Blog… Wonderful Information and really very much useful. Thanks for sharing and keep updating…
    RPA Training in Chennai | RPA Training in Pallikaranai

    ReplyDelete
  51. Awesome article…. Really very helpful information for us. Keep it up. Keep blogging. Looking to reading your next post,. Thank You for Sharing This Information
    Automation Anywhere Training in Chennai | Automation Anywhere Training in Pallavaram

    ReplyDelete
  52. Good Post…. Thanks for the useful information it’s very useful to read your blog. Keep blogging article like this.
    Best JAVA/J2EE Training Institute in Perungudi| Best JAVA/J2EE Training Institute in Velachery

    ReplyDelete
  53. Nice blog, really I feel happy to see this useful blog… Thanks for sharing this valuable information to our vision....
    Best AWS Training Institute in Medavakkam| Best AWS Training Institute in Velachery

    ReplyDelete
  54. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    Blue prism Training in Chennai | Blue prism Training in Besant Nagar

    ReplyDelete
  55. Really I enjoy this blog….. Very nice post… Thanks for sharing and keep updating..
    Ethical Hacking Training in Chennai | Ethical Hacking Training in ST. Thomas Mount

    ReplyDelete
  56. Awesome post…. This is really helpful for me. I like it. Thanks for sharing……
    Best Azure Training Institute in Chennai| Best Azure Training Institute in Velachery

    ReplyDelete

  57. Your blog on RPAis so attractive that i am not able to stop myself to read this blog of yours on RPA.This blog contains all the important topics and points of RPA. Requesting you keep updating this post on RPAand help them who is eager to gain knowledge on RPA.
    Thanks and Regards,
    Uipath training in chennai
    blue prism training institute in chennai
    blue prism training with certification in chennai

    ReplyDelete
  58. Really wonderful post.... My sincere thanks for sharing very useful information to the users... Please continue to share this kind of post
    Best Python Exams Center in OMR| Best Python Certification Exams in OMR

    ReplyDelete
  59. Really wonderful post.... My sincere thanks for sharing very useful information to the users... Please continue to share this kind of post
    Best Python Exams Center in OMR| Best Python Certification Exams in OMR

    ReplyDelete
  60. Really wonderful post.... My sincere thanks for sharing very useful information to the users... Please continue to share this kind of post
    Best Python Exams Center in OMR| Best Python Certification Exams in OMR

    ReplyDelete
  61. Nice post. Thanks for sharing and informing about your services.

    technocrawler
    Technology

    ReplyDelete
  62. Thanks For sharing such a wonderful Blog on RPA. This blog contains so much of data about RPA that anyone who is searching for RPA, its really helpful for them to grab this data from your blog on RPA. Again thank you so much for your blog on RPA.
    Thanks and Regards,
    blue prism training in chennai
    Best blue prism training in chennai
    blue prism training cost in chennai

    ReplyDelete
  63. Thanks for sharing this valuable information.. I saw your website and get more details..Nice work...
    Graphic Designing Training in Chennai | Graphic Designing Training in Keelkattalai

    ReplyDelete
  64. Wonderful blog… You provided very interesting information here. I feel happy to read this post. I hope that you will write many posts like this… Thanks for sharing and Keep updating…..
    Python Training Institute in Chennai | Python Training Institute in Velachery

    ReplyDelete
  65. Your Blog is really awesome with useful and helpful content for us.Thanks for sharing ..keep updating more information..
    UIPath Training in Chennai | UIPath Training in Kanchipuram

    ReplyDelete
  66. Nice Blog… Wonderful Information and really very much useful. Thanks for sharing and keep updating…
    RPA Training in Chennai | RPA Training in Pallavaram

    ReplyDelete