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.
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.
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.
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: