I am trying to write a java program that reads a texfile line by line and store each line in its own array so that these lines will then become data columns. For example, the data.txt file below has the following data:
- 122,80,100,119,162,90,136...
- 122,80,100,119,162,90,136...
- 64,74,70,64,76,62,84,78 ...
- positive,negative,negative ...
I want each line to be stored in its own array, hence 4 lines, 4 different arrays since each line will belong to a specific column. I am able to read the textfile.
BufferedReader br = new BufferedReader(new FileReader("C://data.txt"));
ArrayList lines = new ArrayList();
for(String line = br.readLine();line != null;line = br.readLine()) {
line.replaceAll(",","\\.");
String[] fields = line.split(" ");
System.out.println(" " + fields[0]);
lines.add(fields);
}
String[][] strings = (String[][]) lines.toArray(new String[lines.size()][]);
System.out.println("Total Lines: " + strings.length);
Here's an expected output: