5

I have the following code:

    @POST
    @Path("/csv")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String populateCSV(@FormDataParam("data") InputStream fileInputStream) throws   JsonParseException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        File initialFile = new File("/Users/me/Downloads/file.csv");
        InputStream targetStream = FileUtils.openInputStream(initialFile);
        CSVReader reader = new CSVReader(new InputStreamReader(targetStream), ',', '"', 0);
        CSVReader jerseyReader = new CSVReader(new InputStreamReader(fileInputStream), ',', '"', 0);
        List<String[]> fileAllRows = reader.readAll();
        List<String[]> jerseyAllRows = jerseyReader.readAll();
        return null;
}

jerseyAllRows which is created out of CSVReader that reads Jersey conversion of File to InputStream returns empty lines, while fileAllRows which is created out of a FileInputStream that contains the same file that is being submitted to jersey, returns 3 lines.

What makes the way that Jersey2 reads file create a different InputStream?

I need to post a file to Jersey2 and be able to parse it with OpenCSV

EDITED

If I convert the jersey input stream into String like this:

InputStream is = new ByteArrayInputStream(IOUtils.toString(inputStream).getBytes());
reader = new CSVReader(new InputStreamReader(is), ',', '"', 0);

I do get lines. but that's a waste of memory :( Any idea?

Dejell
  • 13,947
  • 40
  • 146
  • 229

1 Answers1

6

First, you should try uniVocity-parsers CSV parser as it is twice as fast and has many additional features.

Sencond, you should provide the encoding of your input.

Third, you may also need to specify the line separators explicitly

Example using uniVocity-parsers:

File initialFile = new File("/Users/me/Downloads/file.csv");
InputStream targetStream = FileUtils.openInputStream(initialFile);

CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");

CsvParser parser = new CsvParser(settings);
List<String[]> allRows = parser.parseAll(new InputStreamReader(targetStream, "UTF-8"));

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

josliber
  • 43,891
  • 12
  • 98
  • 133
Jeronimo Backes
  • 6,141
  • 2
  • 25
  • 29
  • great. this worked! can I write with this parser? I want to add to that existing file that I am reading another column – Dejell Jan 10 '15 at 22:09
  • Yes you can! Simply use the [CsvWriter](https://github.com/uniVocity/univocity-parsers/blob/master/src/main/java/com/univocity/parsers/csv/CsvWriter.java) class. Check the [tutorial](https://github.com/uniVocity/univocity-parsers/#quick-and-simple-csv-writing-example) – Jeronimo Backes Jan 11 '15 at 08:08
  • Thanks. what is the outputWrite? I tried to send a ByteArrayOutputStream but it didn't accept it – Dejell Jan 12 '15 at 17:36
  • 1
    Just provide a Writer:ByteArrayOutputStream csvResult = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(csvResult, "UTF-8"); CsvWriter csvWriter = new CsvWriter(writer, settings); – Jeronimo Backes Jan 12 '15 at 23:38