Menu

can you use supercsv without a file

2013-01-30
2013-01-31
  • Colin McQueen

    Colin McQueen - 2013-01-30

    I have a form that passes to a server multi-part form data and currently I am creating a file with the contents from the form in order to use supercsv. The data looks like this:

    time(utc),error,low alarm,high alarm,'ION6200 (KWh)','ION6200 demand (kW)','ION6200 rate (instantaneous) (kW)','ION6200 rate min (kW)','ION6200 rate max (kW)','ION6200Reactive (KVARh)','ION6200Reactive demand (kVAR)','ION6200Reactive rate (instantan (kVAR)','ION6200Reactive rate min (kVAR)','ION6200Reactive rate max (kVAR)','Apparent Power (demand) (KVA)','Apparent Power (instantaneous) (KVA)','Power Factor (demand)','Power Factor (instantaneous)','Water Meter (Gallons)','Water Meter rate (Gpm)','Water Meter rate (instantaneous (Gpm)','Water Meter rate min (Gpm)','Water Meter rate max (Gpm)','Gas Meter (CF)','Gas Meter rate (CFm)','Gas Meter rate (instantaneous) (CFm)','Gas Meter rate min (CFm)','Gas Meter rate max (CFm)','-','-','-','-'
    '2004-05-12 15:45:00',0,0,0,141713,,123.288,122.449,123.288,27841,,24.194,24.129,24.194,,125.639,,0.981,0,,,,,0,,,,,,,,
    '2004-05-12 16:00:00',0,0,0,141743,120,123.288,121.622,124.138,27847,24,24.194,24.161,24.194,122.376,125.639,0.981,0.981,0,0,,,,0,0,,,,,,,
    '2004-05-12 16:15:00',0,0,0,141774,124,123.288,122.449,124.138,27853,24,24.194,24.161,24.194,126.301,125.639,0.982,0.981,0,0,,,,0,0,,,,,,,

    Is there a way to use supercsv without a file by doing it all in memory?

    The reason I am asking about this is the client has the file and the server only needs the data not the file itself on the server.

     

    Last edit: Colin McQueen 2013-01-30
  • James Bassett

    James Bassett - 2013-01-30

    Hi Colin, yes that's certainly possible. All of the Super CSV readers accept a Reader - in your case you'll need to use an InputStreamReader to read the CSV file from the posted form data (as I've suggested here).

    If you're not already, I'd recommend using Apache Commons Fileupload which makes it really easy to obtain the InputStream for your uploaded file. BalusC has written an excellent overview on StackOverflow about this.

    I'll look into getting an example onto the Super CSV website for the next release, but if you have any questions in the meantime, just ask :)

     
    • Colin McQueen

      Colin McQueen - 2013-01-31

      Thanks it worked using the InputStreamReader. Instead of using Apache Commons Fileupload, I am using in Servlet 3.0 the getPart method.

      Part part = request.getPart("LOGFILE");
      InputStream inputStream = part.getInputStream();
      InputStreamReader streamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(streamReader);
      ICsvListReader listReader = new CsvListReader(bufferedReader, CsvPreference.STANDARD_PREFERENCE)

       

      Last edit: Colin McQueen 2013-01-31
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.