Create a new Project Java with Maven project name e.g. TestOpenCSV
At Project Files, open pom.xml and add
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dedetok</groupId>
<artifactId>TestOpenCSV</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>17</maven.compiler.release>
<exec.mainClass>com.dedetok.testopencsv.TestOpenCSV</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.9</version> <!-- or latest -->
</dependency>
</dependencies>
</project>
You can use beans to map the result, or manually process the csv files. for data size bigger then 1 millions, there is deference about 1 second, it is better to manually process. Here are code to test, before you use it in production. Feel free to change any the code to meet your requirement.
Classs Radio
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.dedetok.testopencsv;
import com.opencsv.bean.CsvBindByName;
/**
*
* @author dedetok
*/
public class Radio {
@CsvBindByName(column = "country")
private String country;
@CsvBindByName(column = "city")
private String city;
@CsvBindByName(column = "radioname")
private String radioname;
@CsvBindByName(column = "url_logo")
private String url_logo;
@CsvBindByName(column = "url_stream")
private String url_stream;
public String getCountry() { return country; }
public String getCity() { return city; }
public String getRadioname() { return radioname; }
public String getUrl_logo() { return url_logo; }
public String getUrl_stream() { return url_stream; }
}
Main class
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.dedetok.testopencsv;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import java.util.List;
/**
*
* @author dedetok
*/
public class TestOpenCSV {
static String urlString = "https://raw.githubusercontent.com/dedetok/myradiolist/refs/heads/main/myradio_radiolist.csv";
public static void main(String[] args) throws CsvValidationException {
System.out.println("Hello World!");
// start function
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
// Check response
int status = connection.getResponseCode();
if (status == 200) {
// Configure parser: semicolon separator, double-quote as quote char
InputStreamReader reader = new InputStreamReader(connection.getInputStream());
// Start For parsing manual
CSVParser parser = new CSVParserBuilder()
.withSeparator(';')
.withQuoteChar('"')
.build();
//CSVReader csvReader = new CSVReader(reader);
CSVReader csvReader = new CSVReaderBuilder(reader)
.withCSVParser(parser)
//.withSkipLines(1) // skip header if needed
.build();
String[] nextLine;
int i=1;
while ((nextLine = csvReader.readNext()) != null) {
for (String cell : nextLine) {
System.out.print(cell + " | ");
}
i++;
System.out.println();
if (i==5) {
break;
}
}
// End For parsing manual
// start convert directly to list<Radio> using opencsv beans
/*
CsvToBean<Radio> csvToBean = new CsvToBeanBuilder<Radio>(reader)
.withType(Radio.class)
.withSeparator(';')
.withQuoteChar('"')
.withIgnoreLeadingWhiteSpace(true)
.build();
List<Radio> radios = csvToBean.parse();
System.out.println("Loaded " + radios.size() + " radios!");
// debug
for (int i = 0; i < Math.min(5, radios.size()); i++) {
Radio r = radios.get(i);
System.out.println(
r.getCountry() + " | " +
r.getCity() + " | " +
r.getRadioname() + " | " +
r.getUrl_logo() + " | " +
r.getUrl_stream()
);
}
*/
// end convert directly to list<Radio> using opencsv beans
}
} catch (MalformedURLException ex) {
Logger.getLogger(TestOpenCSV.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(TestOpenCSV.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Code is write with collaboration with chatgpt