Skip to content
Snippets Groups Projects
Commit 0375e856 authored by Robert Butora's avatar Robert Butora
Browse files

adds test for csv-metadata parser

parent 07b21f45
No related branches found
No related tags found
No related merge requests found
import com.opencsv.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.List;
import java.util.ArrayList;
import java.io.Reader;
class CsvParser
{
public static void main(String[] args) throws Exception
{
Path filePath = Paths.get(args[0]);
CsvParser parser = new CsvParser();
List<String[]> lines = parser.readLineByLine(filePath);
for(String[] strArr : lines)
{
for(String str : strArr) System.out.print(str + " | ");
System.out.println();
}
}
public List<String[]> readLineByLine(Path filePath) throws Exception
{
CSVParser parser = new CSVParserBuilder()
.withSeparator(',')
.withIgnoreQuotations(true)
.build();
List<String[]> list = new ArrayList<>();
try (Reader reader = Files.newBufferedReader(filePath))
{
try (CSVReader csvReader = new CSVReaderBuilder(reader)
.withSkipLines(0)
.withCSVParser(parser)
.build();
// CSVReader csvReader = new CSVReader(reader)
)
{
String[] line;
while ((line = csvReader.readNext()) != null) {
list.add(line);
}
}
}
return list;
}
}
build:
javac -classpath /home/robi/.m2/repository/com/opencsv/opencsv/5.7.1/opencsv-5.7.1.jar CsvParser.java
run:
java -classpath /home/robi/.m2/repository/com/opencsv/opencsv/5.7.1/opencsv-5.7.1.jar:.:/home/robi/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar CsvParser /srv/vlkb/surveys/survey_populate.csv
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vlkb</groupId>
<artifactId>csv-parser</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>csv matadata parser</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- FIXME needs JAVA_HOME=/usr/java/jdk-17.0.4.1 (java17 manually installed and aletrnatives activated) -->
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.7.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>csv-parser</finalName>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>CsvParser</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins> </build>
</project>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment