Skip to content

Instantly share code, notes, and snippets.

@halolimat
Created October 20, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halolimat/362434158a97dfd12b235f2d30b2047b to your computer and use it in GitHub Desktop.
Save halolimat/362434158a97dfd12b235f2d30b2047b to your computer and use it in GitHub Desktop.
Read CSV files into List of int arrays in Java 8
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.ArrayList;
public class ReadFileClass {
public static void main(String args[]) {
String fileName = "input.txt";
List<int[]> B = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
B = stream.map(line -> line.split(", ")).map(str -> Stream.of(str).mapToInt(Integer::parseInt).toArray()).collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment