Last active
August 15, 2021 15:28
-
-
Save Tuanm/efa1638a869b584ac617b4b7ac296dce to your computer and use it in GitHub Desktop.
Simple CSV Parser for Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
firstname | lastname | phonenumber | birthday | ||
---|---|---|---|---|---|
Do Minh | Tuan | [email protected] | 0969696969 | 13/05/1955 | |
Do Tuan | Minh | [email protected] | 0969696969 | 13/05/1955 | |
Tuan Do | Minh | [email protected] | 0969696969 | 13/05/1955 | |
Tuan Minh | Do | [email protected] | 0969696969 | 13/05/1955 | |
Minh Tuan | Do | [email protected] | 0969696969 | 13/05/1955 | |
Minh Do | Tuan | [email protected] | 0969696969 | 13/05/1955 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.function.Supplier; | |
public class CSV { | |
private static String COMMA_DELIMITER = ","; | |
private Map<String, List<String>> properties; | |
private int count; | |
private CSV() { | |
properties = new LinkedHashMap<>(); | |
count = 0; | |
} | |
public List<?> as(Supplier<?> constructor) throws IndexOutOfBoundsException, | |
IllegalAccessException, IllegalArgumentException { | |
var instances = new ArrayList<>(); | |
for (var index = 0; index < count; index++) { | |
var instance = constructor.get(); | |
var clazz = instance.getClass(); | |
var fields = clazz.getDeclaredFields(); | |
for (var field : fields) { | |
field.setAccessible(true); | |
var type = field.getType(); | |
for (var property : properties.keySet()) { | |
if (property.equalsIgnoreCase(field.getName())) { | |
var value = properties.get(property).get(index); | |
try { | |
field.set(instance, type.cast(value)); | |
} catch (ClassCastException e) { | |
if (type.equals(java.time.LocalDate.class)) { | |
// TODO: cast value from java.lang.String to java.time.LocalDate | |
// field.set(instance, java.time.LocalDate.parse(value)); | |
} | |
} | |
} | |
} | |
} | |
instances.add(instance); | |
} | |
return instances; | |
} | |
public static CSV from(String filePath) throws IOException, IndexOutOfBoundsException { | |
var csv = new CSV(); | |
try (var reader = new BufferedReader(new FileReader(filePath))) { | |
String firstLine = reader.readLine(); | |
for (var word : firstLine.split(COMMA_DELIMITER)) { | |
csv.properties.put( | |
word.toString().trim().toLowerCase(), | |
new ArrayList<String>()); | |
} | |
String line; | |
var properties = csv.properties.keySet(); | |
while ((line = reader.readLine()) != null) { | |
var values = line.split(COMMA_DELIMITER); | |
var currentIndex = 0; | |
for (var property : properties) { | |
csv.properties.get(property) | |
.add(values[currentIndex++].trim()); | |
} | |
csv.count++; | |
} | |
} catch (FileNotFoundException e) { | |
throw e; | |
} | |
return csv; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String[] args) throws Exception { | |
var buddies = CSV.from("buddies.csv").as(Buddy::new); | |
buddies.forEach(buddy -> { | |
System.out.println(((Buddy)buddy).getFullName()); | |
System.out.println(((Buddy)buddy).getEmail()); | |
System.out.println(((Buddy)buddy).getPhoneNumber()); | |
System.out.println(((Buddy)buddy).getBirthday()); | |
System.out.println("---"); | |
}); | |
} | |
} | |
class Buddy { | |
private static String SPACE_DELIMITER = " "; | |
private String firstName; | |
private String lastName; | |
private String email; | |
private String phoneNumber; | |
private java.time.LocalDate birthday; | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
public String getFirstName() { | |
return firstName; | |
} | |
public String getLastName() { | |
return lastName; | |
} | |
public String getFullName() { | |
return firstName + SPACE_DELIMITER + lastName; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setPhoneNumber(String phoneNumber) { | |
this.phoneNumber = phoneNumber; | |
} | |
public String getPhoneNumber() { | |
return phoneNumber; | |
} | |
public void setBirthday(java.time.LocalDate birthday) { | |
this.birthday = birthday; | |
} | |
public java.time.LocalDate getBirthday() { | |
return birthday; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment