本文整理匯總了Java中com.opencsv.CSVReader類的典型用法代碼示例。如果您正苦於以下問題:Java CSVReader類的具體用法?Java CSVReader怎麽用?Java CSVReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
CSVReader類屬於com.opencsv包,在下文中一共展示了CSVReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: loadPageFromFile
import com.opencsv.CSVReader; //導入依賴的package包/類
public IPage loadPageFromFile(File file) {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
CSVReader reader = new CSVReader(bufferedReader);
String[] firstLine = reader.readNext();
String pageName = firstLine[0];
String pageId = firstLine[2];
reader.readNext(); //skip line with column name
IPage result = new Page(pageName, pageId);
StreamSupport.stream(reader.spliterator(), false)
.map(cells -> getIElement(file, cells))
.forEach(result::addElement);
return result;
} catch (IOException e) {
throw new RuntimeException("Can't parse file " + file.getName(), e);
}
}
示例2: getHourlyData
import com.opencsv.CSVReader; //導入依賴的package包/類
private static TimeSeries getHourlyData() throws IOException {
final String path = "./fortran_benchmark/hourly_stl_test.csv";
CSVReaderBuilder builder = new CSVReaderBuilder(new FileReader(path));
TimeSeries ts = new TimeSeries();
try (CSVReader reader = builder.build()) {
String[] nextLine;
long time = 1492457959000L;
while ((nextLine = reader.readNext()) != null) {
ts.times.add(time);
time += 3600 * 1000;
double value = Double.parseDouble(nextLine[0]);
ts.values.add(value);
}
}
return ts;
}
示例3: processSamples
import com.opencsv.CSVReader; //導入依賴的package包/類
private void processSamples() throws IOException {
for (int i = 0; i < SAMPLE_FILES.length; i++) {
String file = SAMPLE_FILES[i];
currentActivity = SAMPLE_CLASSIFIER[i].ordinal();
sampleCount = 0;
CSVReader reader = new CSVReader(new FileReader(SAMPLE_DIR + file));
String[] nextLine;
featureGenerator.reset();
while ((nextLine = reader.readNext()) != null) {
long ts = Long.parseLong(nextLine[0]);
float x = Float.parseFloat(nextLine[1]);
float y = Float.parseFloat(nextLine[2]);
float z = Float.parseFloat(nextLine[3]);
featureGenerator.addAccelerometerReading(ts, x, y, z);
}
}
}
示例4: readTestData
import com.opencsv.CSVReader; //導入依賴的package包/類
private static List<TrainingData> readTestData(String fn) {
int[] shape = { 3, 1 };
List<TrainingData> trainingDataSet = new ArrayList<>();
try {
CSVReader reader = new CSVReader(new FileReader(fn));
String[] row;
while ((row = reader.readNext()) != null) {
int type = Integer.parseInt(row[0]);
double f1 = Double.parseDouble(row[1]);
double f2 = Double.parseDouble(row[2]);
double f3 = Double.parseDouble(row[3]);
TrainingData trainingData = new TrainingData();
trainingData.input = Nd4j.create(new double[] { f1, f2, f3 }, shape);
trainingData.output = Nd4j.zeros(shape);
trainingData.output.putScalar(type, (double) 1);
trainingDataSet.add(trainingData);
}
} catch (java.io.IOException e) {
}
return trainingDataSet;
}
示例5: importCSV
import com.opencsv.CSVReader; //導入依賴的package包/類
public static void importCSV(String csv, boolean cleanBeforeImport) {
LOG.info("Importing CSV. Cleanup old data before import: {}", cleanBeforeImport);
CSVReader csvReader = CSVUtils.createDefaultReader(csv);
try {
if (cleanBeforeImport) {
ElasticSearch.getNamedQueryOperations().deleteAll();
}
List<String[]> data = csvReader.readAll();
Map<String, Integer> columnIndexes = CSVUtils.resolveColumnIndexes(CSV_COLUMNS, data.get(0));
data.stream()
.skip(1)
.map(row -> mapCsvRowToNamedQuery(row, columnIndexes))
.forEach(nq -> ElasticSearch.getNamedQueryOperations().save(nq));
LOG.info("Imported {} rows", data.size() - 1);
} catch (IOException e) {
e.printStackTrace();
}
}
示例6: importCSV
import com.opencsv.CSVReader; //導入依賴的package包/類
public static void importCSV(String csv, boolean cleanBeforeImport) {
LOG.info("Importing CSV. Cleanup old data before import: {}", cleanBeforeImport);
CSVReader csvReader = CSVUtils.createDefaultReader(csv);
try {
if (cleanBeforeImport) {
ElasticSearch.getHttpSourceOperations().deleteAll();
}
List<String[]> data = csvReader.readAll();
String[] headerLine = data.get(0);
Map<String, Integer> columnIndexes = resolveColumnIndexes(headerLine, CSV_COLUMNS);
data.stream()
.skip(1)
.map(row -> mapCsvRowToHttpSource(row, columnIndexes))
.forEach(hs -> ElasticSearch.getHttpSourceOperations().save(hs));
LOG.info("Imported {} rows", data.size() - 1);
} catch (IOException e) {
e.printStackTrace();
}
}
示例7: importCSV
import com.opencsv.CSVReader; //導入依賴的package包/類
public static void importCSV(String csv, boolean cleanBeforeImport) {
LOG.info("Importing CSV. Cleanup old data before import: {}", cleanBeforeImport);
CSVReader csvReader = CSVUtils.createDefaultReader(csv);
try {
if (cleanBeforeImport) {
ElasticSearch.getHttpSourceTestOperations().deleteAll();
}
List<String[]> data = csvReader.readAll();
String[] headerLine = data.get(0);
Map<String, Integer> columnIndexes = resolveColumnIndexes(headerLine, CSV_COLUMNS);
data.stream()
.skip(1)
.map(row -> mapCsvRowToHttpSourceTest(row, columnIndexes))
.forEach(hst -> ElasticSearch.getHttpSourceTestOperations().save(hst));
LOG.info("Imported {} rows", data.size() - 1);
} catch (IOException e) {
e.printStackTrace();
}
}
示例8: readLine
import com.opencsv.CSVReader; //導入依賴的package包/類
/**
* {@inheritDoc}
*/
@Override
public String[] readLine(int line, boolean readResult) {
logger.debug("readLine at line {}", line);
try {
CSVReader reader = openInputData();
List<String[]> a = reader.readAll();
if (line >= a.size()) {
return null;
}
String[] row = a.get(line);
if ("".equals(row[0])) {
return null;
} else {
String[] ret = readResult ? new String[columns.size()] : new String[columns.size() - 1];
for (int i = 0; i < ret.length; i++) {
ret[i] = row[i];
}
return ret;
}
} catch (IOException e) {
logger.error("error CsvDataProvider.readLine()", e);
return null;
}
}
示例9: initColumns
import com.opencsv.CSVReader; //導入依賴的package包/類
private void initColumns() throws EmptyDataFileContentException, WrongDataFileFormatException, IOException {
columns = new ArrayList<>();
CSVReader reader = openInputData();
String[] headers = reader.readNext();
for (String header : headers) {
if (!"".equals(header)) {
columns.add(header);
}
}
reader.close();
if (columns.size() < 2) {
throw new EmptyDataFileContentException(Messages.getMessage(EmptyDataFileContentException.EMPTY_DATA_FILE_CONTENT_ERROR_MESSAGE));
}
resultColumnName = columns.get(columns.size() - 1);
if (!isResultColumnNameAuthorized(resultColumnName)) {
throw new WrongDataFileFormatException(String.format(Messages.getMessage(WrongDataFileFormatException.WRONG_RESULT_COLUMN_NAME_ERROR_MESSAGE), ResultColumnNames.getAuthorizedNames()));
}
}
示例10: parseUrlATMO
import com.opencsv.CSVReader; //導入依賴的package包/類
public static PointMesure[] parseUrlATMO(URL url) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
CSVReader reader = new CSVReader(in,';');
List<String[]> contenuCsv = reader.readAll();
String[] date = contenuCsv.get(0);
String[] indice = contenuCsv.get(1);
PointMesure[] PointMesures = new PointMesure[indice.length];
for (int i = 0; i < PointMesures.length ; i++) {
PointMesures[i] = new PointMesure(date[i], indice[i]);
}
//inverser les éléments du tableau
for(int i = 0; i < PointMesures.length / 2; i++)
{
PointMesure temp = PointMesures[i];
PointMesures[i] = PointMesures[PointMesures.length - i - 1];
PointMesures[PointMesures.length - i - 1] = temp;
}
return PointMesures;
}
示例11: getCo2Data
import com.opencsv.CSVReader; //導入依賴的package包/類
@SuppressWarnings("Duplicates")
private static TimeSeries getCo2Data() throws IOException {
final String path = "../StlDemoRestServer/co2.csv";
CSVReaderBuilder builder = new CSVReaderBuilder(new FileReader(path));
TimeSeries ts = new TimeSeries();
try (CSVReader reader = builder.withSkipLines(1).build()) {
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
double dateAsYear = Double.parseDouble(nextLine[1]);
long time = (long) ((dateAsYear - 1970.0) * 365.25 * 24 * 60 * 60 * 1000);
ts.times.add(time);
double value = Double.parseDouble(nextLine[2]);
ts.values.add(value);
}
}
return ts;
}
示例12: getTimeSeries
import com.opencsv.CSVReader; //導入依賴的package包/類
@SuppressWarnings("Duplicates")
public static TimeSeries getTimeSeries(String fileName) throws IOException {
CSVReaderBuilder builder = new CSVReaderBuilder(new FileReader(fileName));
TimeSeries ts = new TimeSeries();
try (CSVReader reader = builder.withSkipLines(1).build()) {
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
double dateAsYear = Double.parseDouble(nextLine[1]);
long time = (long) ((dateAsYear - 1970.0) * 365.25 * 24 * 60 * 60 * 1000);
ts.times.add(time);
double value = Double.parseDouble(nextLine[2]);
ts.values.add(value);
}
}
return ts;
}
示例13: parse
import com.opencsv.CSVReader; //導入依賴的package包/類
private UserAgentParser parse(final Reader input) throws IOException, ParseException {
final List<Rule> rules = new ArrayList<>();
try (final CSVReader csvReader = new CSVReader(input)) {
final Iterator<String[]> iterator = csvReader.iterator();
while (iterator.hasNext()) {
final String[] record = iterator.next();
final Rule rule = getRule(record);
if (rule != null) {
rules.add(rule);
}
}
}
return new UserAgentParserImpl(rules.toArray(new Rule[0]), getDefaultCapabilities());
}
示例14: readStockDataFromFile
import com.opencsv.CSVReader; //導入依賴的package包/類
private List<StockData> readStockDataFromFile (String filename, String symbol) {
List<StockData> stockDataList = new ArrayList<>();
try {
for (int i = 0; i < maxArray.length; i++) { // initialize max and min arrays
maxArray[i] = Double.MIN_VALUE;
minArray[i] = Double.MAX_VALUE;
}
List<String[]> list = new CSVReader(new FileReader(filename)).readAll(); // load all elements in a list
for (String[] arr : list) {
if (!arr[1].equals(symbol)) continue;
double[] nums = new double[VECTOR_SIZE];
for (int i = 0; i < arr.length - 2; i++) {
nums[i] = Double.valueOf(arr[i + 2]);
if (nums[i] > maxArray[i]) maxArray[i] = nums[i];
if (nums[i] < minArray[i]) minArray[i] = nums[i];
}
stockDataList.add(new StockData(arr[0], arr[1], nums[0], nums[1], nums[2], nums[3], nums[4]));
}
} catch (IOException e) {
e.printStackTrace();
}
return stockDataList;
}
示例15: RawTafFile
import com.opencsv.CSVReader; //導入依賴的package包/類
/**
* Parse d'un fichier Csv
* @param reader :CSVReader
* @throws IOException
*/
public RawTafFile(CSVReader reader) throws IOException {
logger.debug("IN");
String[] rawLine;
int i = 0;
int j = 0;
while ((rawLine = reader.readNext()) != null) {
List<String> parsedline = new ArrayList<String>();
for (String cell : rawLine) {
if(j == 0 && i != 0){//Première ligne
if(cell != null && !cell.equals("")){
columnsMap.put(i, cell);
}
}else if(i == 0 && j != 0){//Première colonne
if(cell != null && !cell.equals("")){
rowsMap.put(j, cell);
}
}
parsedline.add(cell == null ? "" : cell);
i++;
}
content.add(parsedline);
i=0;
j++;
}
}