• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 3065
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!
 
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic