Java read CSV File 📰

Описание к видео Java read CSV File 📰

IMPORTANT NOTE: If values in your CSV contain commas naturally, they will be split. (Ex. $1,000). If this is the case, replace:

String[] row = line.split(",");
with
String[] row = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");

^ This is regex and is very complicated. It looks for string patterns. Explaining this requires another video entirely.
//******************************************************
import java.io.*;

public class Main {

public static void main(String[] args) {

//CSV = Comma-Separated Values
// text file that uses a comma to separate values

String file = "src\\students.csv";
BufferedReader reader = null;
String line = "";

try {
reader = new BufferedReader(new FileReader(file));
while((line = reader.readLine()) != null) {

String[] row = line.split(",");
//String[] row = line.split(",(?=([^\"]*\"[^\"]*\")*[^\"]*$)");
//use this if your values already contain commas
for(String index : row) {
System.out.printf("%-10s", index);
}
System.out.println();
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//******************************************************

Комментарии

Информация по комментариям в разработке