-2

I am trying to read a txt file in java. I have to separate each line of the code and put each individual line into a new array object comprised of each number, which are separated by commas. The file is long but to summarize it looks like this.

1,2,12343,12422,12342,12322,12421
2,3,12322,42444,24344,24553,34535

How would I write the code to add each line into an array with that lines numbers as its content?

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140

1 Answers1

1

Using nio Path, Files and streams:

Path path = Path.of(filename);
List<int[]> arrays = Files.lines(path)
    .map(line -> line.split(","))
    .map(split -> Stream.of(split).mapToInt(Integer::parseInt).toArray())
    .collect(Collectors.toList());
boot-and-bonnet
  • 731
  • 2
  • 5
  • 16