It's not a secret anymore!*
The moose likes Struts and the fly likes Struts FormFile (parsing .csv files) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts FormFile (parsing .csv files)" Watch "Struts FormFile (parsing .csv files)" New topic
Author

Struts FormFile (parsing .csv files)

Daniel McDade
Greenhorn

Joined: Mar 21, 2006
Posts: 11
I am allowing our users to upload .csv files, but was having a little trouble deciding how to parse the file content. Normally, I'd wrap the .csv file in a BufferedReader and read it line by line, but with a FormFile object, I only have an InputStream to read the file data. I'm going to add each line in the file to a List object to save to a database.

I'm not a big I/O API expert, so does someone have a clean way to read the .csv data using the Struts FormFile object?

Thanks for any help,
D
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

InputStream -> InputStreamReader -> BufferedReader


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Daniel McDade
Greenhorn

Joined: Mar 21, 2006
Posts: 11


Joe, so something like the following should work? Thanks for your help.


I've been told users can also upload the data as a .txt file, but I'm thinking this should work under that scenario, too.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A CSV file *is* a text file.
Daniel McDade
Greenhorn

Joined: Mar 21, 2006
Posts: 11
David Newton wrote:A CSV file *is* a text file.


True enough. :=)
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Daniel McDade wrote:



Do not do this. ready() does not tell you if the stream has ended, it tells you if a read call will block:

Returns:
True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.


javadoc
Daniel McDade
Greenhorn

Joined: Mar 21, 2006
Posts: 11
Joe Ess wrote:Do not do this. ready() does not tell you if the stream has ended, it tells you if a read call will block:


Joe, based on what I saw, the following would be a better approach, correct?


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8836
    
    7

Daniel McDade wrote:
Joe, based on what I saw, the following would be a better approach, correct?


Yes, that's how I'd do it
 
wood burning stoves
 
subject: Struts FormFile (parsing .csv files)
 
Similar Threads
Dynaforms, Validator and File Upload
Uploading and Downloading a file
Header issue for DailyRollingFileAppender when rolled over
How to convert FormFile into BLOB object?
Struts File tag