• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I get a FileReader from an InputStream?

 
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm using Java 1.6. How can I get a FileReader object from an InputStream object?

The origin of this problem is that I have a Spring 3 controller that passes me a MultipartFile object, which has a "getInputStream" method and I'm trying to create another object (CsvBeanReader), whose constructor takes a FileReader object.

Thanks, - Dave
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Spring MultiPartFile doesn't necessarily point to a file, it may point to RAM. It is used to point to the contents of a data stream uploaded to the Server from a Client file. There is no way to convert the InputStream into a FileReader. What you would need to do is write the contents of the MultiPartFile into a real on-disk File, then open a FileReader to it.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can certainly use an InputStreamReader to wrap a Reader around an InputStream. If you don't have another constructor that takes a plain Reader though, you might be out of luck, because you don't have an actual file to open. You might be able to subclass FileReader to bypass the file opening stuff, but that would be a hack.
 
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know it's an old topic, but I'll post my answer just for the record. It might help.
Having dealt with CSV readers lately I was facing the same issue while trying to parse CSV file coming from a file upload.

CVSReader's API was expecting FileReader for e.g. from a local file path:


But I needed CVSReader to read InputStream so all I did was:


In my case file was of type MultipartFile coming from the multipart/form-data upload form.

Thanks!
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic