Parse report CSV file with Java

You can specify how to parse the Reports CSV files using the Java code that we provide.

Parse CSV file with Java

To format the CSV file, use this Java code to parse your response to a CSV file. This example demonstrates using the header and the body to process the response.

Example: Java code to parse CSV file

if (response.getStatus() == Response.Status.OK.getStatusCode() && response.hasEntity()) { InputStream inputStream = (InputStream)response.getEntity(); try { String header = response.getHeaderString("Content-Disposition"); if(header != null && !("").equals(header)) { if(header.contains("filename")){ //header value will be something like: //attachment; filename=10000000354_2016-01-15T23:09:54.438+0000.zip int length = header.length(); String fileName = header.substring(header.indexOf("filename="),length); System.out.println("filenameText " + fileName); String [] str = fileName.split("="); System.out.println("fileName: " + str[1]); //replace "/Users/anauti1/Documents/" below with your values File reportFile = new File("/Users/anauti1/Documents/" + str[1].toString()); OutputStream outStream = new FileOutputStream(reportFile); byte[] buffer = new byte[8 * 1024]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outStream); } } } catch (Exception ex){ System.out.print("Exception: " + ex.getMessage()); }
}