Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts FormFile (parsing .csv files)

 
Daniel McDade
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
 
Daniel McDade
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Ess wrote:InputStream -> InputStreamReader -> BufferedReader


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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A CSV file *is* a text file.
 
Daniel McDade
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:A CSV file *is* a text file.


True enough. :=)
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic