Hi Roderick,
here’s a simple demo that reads a CSV file into an array of strings. Each item in the file becomes one array element. On my setup (java on Windows using notepad and command line compiling and executing) the file is compiled then executed with:
javac readfile.java
java -cp . readfile
and “thefile.csv” is in the same directory as the complied .class file
import java.io.BufferedReader;
import java.io.FileReader;
public class readfile {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("thefile.csv"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (String str: values) {
System.out.println(str);
}
}
br.close();
}
}
The code comes from http://www.java2s.com which has lots of demos.
Paul
(Pasting code here might lose the indentation but https://codebeautify.org/javaviewer will resore the layout)