You can construct an InputStreamReader from that InputStream
new InputStreamReader(myInputStream, encoding)
Where myInputStream is your InputStream and encoding is a String that defines the encoding used by your datasource.
You can call your CSVReader like this:
new CSVReader(new InputStreamReader(myInputStream, encoding));
Answer from oers on Stack OverflowApache Commons
commons.apache.org › proper › commons-csv › apidocs › org › apache › commons › csv › CSVParser.html
CSVParser (Apache Commons CSV 1.14.2-SNAPSHOT API)
public static CSVParser parse · (InputStream inputStream, Charset charset, CSVFormat format) throws IOException · Creates a CSV parser using the given CSVFormat. If you do not read all records from the given reader, you should call close() on the parser, unless you close the reader.
Java2s
java2s.com › example › java-utility-method › csv-file-parse › parsecsv-inputstream-csvinput-3765b.html
Java CSV File Parse parseCsv(InputStream csvInput)
*/ public static List<List<String>> parseCsv(InputStream csvInput) { return parseCsv(csvInput, DEFAULT_CSV_SEPARATOR); } /** * CSV content parser. Convert an InputStream with the CSV contents to a two-dimensional List * of Strings representing the rows and columns of the CSV.
Oracle
docs.oracle.com › middleware › 1213 › adf › api-reference-model › oracle › adf › model › adapter › dataformat › csv › CSVParser.html
CSVParser (Oracle Fusion Middleware Java API Reference for Oracle ADF Model)
public CSVParser(java.io.InputStream pInputStream) throws java.lang.Exception · Constructor · Parameters: pInputStream - CSV input stream · Throws: java.lang.Exception - any error occurred · public CSVParser(java.io.InputStream pInputStream, java.lang.String pEnc) throws java.lang.Exception ·
Apache Commons
commons.apache.org › proper › commons-csv › jacoco › org.apache.commons.csv › CSVParser.java.html
CSVParser.java - Apache Commons
* @since 1.5 */ public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format) throws IOException { return parse(new InputStreamReader(inputStream, Charsets.toCharset(charset)), format); } /** * Creates and returns a parser for the given {@link Path}, which the caller MUST close.
Top answer 1 of 2
12
Use
CSVReader reader = new CSVReader(new InputStreamReader(input));
Edit to answer the comment
The problem is because you don't have any constructor for FileReader as you want. See the FileReader API for details. But since you will have to convert the byte stream to character stream so you will have to use InputStreamReader. For better performance you can wrap it with a BufferedReader also
2 of 2
10
CSVReader reader = new CSVReader(new InputStreamReader(input, "UTF-8"));
The bridge between binary bytes / InputStream, and unicode text (Reader/String) is the InputStreamReader. Specify the encoding of the bytes / InputStream if the file is not local.
Java Tips
javatips.net › api › org.apache.commons.csv.csvparser
Java Examples for org.apache.commons.csv.CSVParser
The following java examples will help you to understand the usage of org.apache.commons.csv.CSVParser. These source code samples are taken from different open source projects. ... @Override public void map(Text key, BytesWritable value, Context context) throws IOException, InterruptedException { ByteArrayInputStream inputStream = new ByteArrayInputStream(value.getBytes()); int n = value.getLength(); byte[] bytes = new byte[n]; inputStream.read(bytes, 0, n); CSVParser parser = CSVParser.parse(new String(bytes), CSVFormat.DEFAULT); try { for (CSVRecord csvRecord : parser) { for (String string : csvRecord) { word.set(string); context.write(word, one); Counter counter = context.getCounter(CountersEnum.class.getName(), CountersEnum.INPUT_WORDS.toString()); counter.increment(1); } } } catch (Exception e) { } }
Simplesolution
simplesolution.dev › java-read-and-parse-csv-file-using-apache-commons-csv
Read and Parse CSV File in Java using Apache Commons CSV
args) { try { String csvFileName = "D:\\SimpleSolution\\Customers.csv"; CSVFormat csvFormat = CSVFormat.DEFAULT.withFirstRecordAsHeader().withIgnoreHeaderCase(); InputStream inputStream = new FileInputStream(csvFileName); CSVParser csvParser = CSVParser.parse(inputStream, StandardCharsets.UTF_8, csvFormat); for(CSVRecord csvRecord : csvParser) { String firstName = csvRecord.get("First Name"); String lastName = csvRecord.get("Last Name"); String email = csvRecord.get("Email"); String phoneNumber = csvRecord.get("Phone Number"); System.out.println(firstName + "," + lastName + "," + email + "," + phoneNumber); } inputStream.close(); csvParser.close(); } catch (IOException e) { e.printStackTrace(); } } } The output is:
Apache Commons
commons.apache.org › proper › commons-csv › apidocs › src-html › org › apache › commons › csv › CSVParser.html
Source code - Apache Commons
345 * @since 1.5 346 */ 347 public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format) 348 throws IOException { 349 return parse(new InputStreamReader(inputStream, Charsets.toCharset(charset)), format); 350 } 351 352 /** 353 * Creates and returns a parser for the given {@link Path}, which the caller MUST close.
GitHub
github.com › apache › commons-csv › blob › master › src › main › java › org › apache › commons › csv › CSVParser.java
commons-csv/src/main/java/org/apache/commons/csv/CSVParser.java at master · apache/commons-csv
public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format)
Author apache
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Bennadel
bennadel.com › blog › 2241-parsing-csv-data-with-an-input-stream-and-a-finite-state-machine.htm
Parsing CSV Data With An Input Stream And A Finite State Machine
April 21, 2020 - ---> <cfset var local = {} /> <!--- Even if the document has no data, we will, at the very least, start and end the document. ---> <cfset this.publish( "startDocument" ) /> <!--- Read the first character in the CSV input stream. ---> <cfset local.nextByte = variables.inputStream.read() /> <!--- The input stream will be providing a single byte at a time.
SAP
help.sap.com › doc › 02d5152884b34821a06408495ba0b771 › 1905 › en-US › de › hybris › platform › util › CSVReader.html
CSVReader (hybris Commerce Suite 1905)
Create the Reader csvreader = new CSVReader(inputStream, null ); //null for default encoding Use the constructor to set the encoding and the stream.
GitHub
github.com › motech › test › blob › master › src › test › java › org › motechproject › whp › mtraining › csv › parser › CsvParserTest.java
test/src/test/java/org/motechproject/whp/mtraining/csv/parser/CsvParserTest.java at master · motech/test
List<CourseStructureCsvRequest> actualCourseCsvContent = csvParser.parse(mockMultipartFile, CourseStructureCsvRequest.class); ... public void shouldParseGivenCsvFileToGivenBeanTypeIgnoringExtraColumnOfCsv() throws IOException { String resourceName = "fileWithExtraHeading.csv"; File file = new File("./src/test/resources/fileWithExtraHeading.csv"); InputStream inputStream = new FileInputStream(file);
Author motech
Stack Overflow
stackoverflow.com › questions › 51744690 › apache-commons-csv-parser
java 8 - Apache commons CSV parser - Stack Overflow
CSVParser.parse(inputStream, Charset.defaultCharset(), CSVFormat.DEFAULT .withTrim() .withHeader() // note, do not specify any columns here )
Stack Overflow
stackoverflow.com › questions › 39520459 › convert-csvwriter-to-inputstream
csv - Convert CSVwriter to inputstream - Stack Overflow
May 28, 2020 - The solution is to use InputStreamReader to read the file and pass that stream to Buffered reader and read line by line or as you want.
Mkyong
mkyong.com › home › java › how to read and parse csv file in java
How to read and parse CSV file in Java - Mkyong.com
December 26, 2020 - package com.mkyong.io.csv; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.net.URL; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; public class CsvParserSimple { private static final char DEFAULT_SEPARATOR = ','; private static final char DOUBLE_QUOTES = '"'; private static final char DEFAULT_QUOTE_CHAR = DOUBLE_QUOTES; private static final String NEW_LINE = "\n"; private boolean isMultiLine = false; private String pendingField = ""; private String[] pendingFieldLine = new String[]{}; public static void main(String[] args) throws Exception { // loads CSV file from the resource folder.