Reputation: 55
my CSV file looks like this:
Id;date;code;type;category;name;position;formula
10;;;2010-02-01;;000010;P;W;NormalDays;10;#formelTest
55;;;2050-05-02;;000055;D;C;SpecificDays;55;#formelTest2
60;;;2301-08-03;;000060;A;C;NotNormalDays;60;#formelBlablblabla
75;;;2012-01-08;;000075;P;W;VulgaryDays;75;@formellbalbalbalbababa
I would like to read from it lines by line only from only first column id and last column formula. How can I do this? Thanks for help.
The code:
File file = new File("Filename.csv");
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (String line : lines)
{
String[] array = line.split(",");
System.out.println(array[0]);
}
Upvotes: 1
Views: 17829
Reputation: 1
String filename = "matches.csv";
File file = new File(filename);
try {
Scanner sc = new Scanner(file);
while (sc.hasNext()) {
String data = sc.next();
String[] values = data.split(",");
System.out.println(data);
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Upvotes: 0
Reputation: 2041
Completing your code:
File file = new File("Filename.csv");
List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
for (String line : lines) {
String[] array = line.split(";");
System.out.println(array[0]+" "+array[array.length-1]);
}
You will notice that I changed the comma to a semi-colon as the split separator and added a bit to the println
, which will be the last item in your split array.
I hope it helps.
Upvotes: 3