Java read CSV File 📰

Veröffentlicht am: 06 Juli 2020
auf dem Kanal: Bro Code
94,128
2.1k

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();
}
}
}
}
//******************************************************


Auf dieser Seite können Sie das Online-Video Java read CSV File 📰 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Bro Code 06 Juli 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 94,128 Mal angesehen und es wurde von 2.1 tausend den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!