Created
October 20, 2019 22:58
-
-
Save halolimat/362434158a97dfd12b235f2d30b2047b to your computer and use it in GitHub Desktop.
Read CSV files into List of int arrays in Java 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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