Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.github.timo_reymann.csv_parser.io;
import com.github.timo_reymann.csv_parser.meta.CsvMetaDataReader;
import com.github.timo_reymann.csv_parser.util.Converter;
import lombok.AccessLevel;
import lombok.Setter;
import java.io.*;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* Reader for csv files
*
* @author Timo Reymann
* @since 20.12.17
*/
public class CsvReader implements AutoCloseable, Flushable, Closeable {
/**
* Underlying file reader
*/
@Setter(AccessLevel.PROTECTED)
private FileReader fileReader;
/**
* Buffered reader for reading lines
*/
@Setter(AccessLevel.PROTECTED)
private BufferedReader bufferedReader;
/**
* Class object for type to read from file
*/
@Setter(AccessLevel.PROTECTED)
private Class clazz;
/**
* CSV file to read
*/
@Setter(AccessLevel.PROTECTED)
private File file;
/**
* Csv file has header in first line
*/
private boolean hasHeading = false;
/**
* Seperator for csv file
*/
private String seperator = Seperator.SEMICOLON;
/**
* Headings for file, if headings detection is enabled for this file
*/
private HashMap headings = new HashMap<>();
/**
* Meta data api
*/
private CsvMetaDataReader csvMetaDataReader;
/**
* Converter api
*/
private final Converter converter = new Converter();
/**
* Create CsvReader
*
* @param file File to read
* @param clazz Class of bean to read
* @param hasHeading Has the file headers for column names
* @throws FileNotFoundException File was not found on disk
*/
public CsvReader(File file, Class clazz, boolean hasHeading) throws FileNotFoundException {
this.file = file;
this.clazz = clazz;
this.csvMetaDataReader = new CsvMetaDataReader<>(clazz);
this.setHasHeading(hasHeading);
init();
}
/**
* Create new instance
*
* @param fileName Name of file
* @param clazz Class of bean
* @throws FileNotFoundException File was not found on disk
*/
public CsvReader(String fileName, Class clazz) throws FileNotFoundException {
this(new File(fileName), clazz, false);
}
/**
* Create new instance
*
* @param fileName Name of file
* @param clazz Class of bean
* @param hasHeading File has first line with headings
* @throws FileNotFoundException File was not found on disk
*/
public CsvReader(String fileName, Class clazz, boolean hasHeading) throws FileNotFoundException {
this(new File(fileName), clazz, hasHeading);
}
/**
* Map splitted data of line to object of bean
*
* @param data Data to map
* @return Mapped object
* @throws InstantiationException Error creating bean instance, this occurs when
* no default constructor without parameters is available or an exception is thrown during initalization
* @throws IllegalAccessException Constructor is private
*/
private T map(String[] data) throws InstantiationException, IllegalAccessException {
if (hasHeading) {
return mapByHeading(data);
}
return mapByIndex(data);
}
/**
* Map bean by heading in annotations
*
* @param data Data to set
* @return Mapped bean instance
* @throws IllegalAccessException Constructor is private
* @throws InstantiationException Error creating bean instance, this occurs when
* no default constructor without parameters is available or an exception is thrown during initalization
*/
private T mapByHeading(String[] data) throws IllegalAccessException, InstantiationException {
T obj = clazz.newInstance();
HashMap