Вы должны использовать это регулярное выражение [, ]
для разбиения вашей строки, поскольку значения в вашей строке разделены запятой или пробелом.Попробуйте ваш измененный код Java, используя,
String knownHostsPath = "target/test/known_hosts";
try (Stream<String> stream = Files.lines(Paths.get(knownHostsPath))) {
List<String[]> list = stream
.map(x -> x.split("[, ]")) // use map to split each line into array of strings using [, ] regex which means either space or comma
.filter(x -> x.length == 4) // use filter to retain only values which have four values after splitting to get rid away of junk lines
.collect(Collectors.toList()); // collect the string array containing your four (host, ip, keytype, key) values as list
list.forEach(x -> { // print the values or do whatever
System.out.println(String.format("Host: %s, IP: %s, KeyType: %s, Key: %s", x[0], x[1], x[2], x[3]));
});
}
Печатает следующее,
Host: my_host, IP: 0.0.0.0, KeyType: ssh-rsa, Key: xxxxxxxx
Host: my_host1, IP: 1.2.3.4, KeyType: ssh-rsa1, Key: yyyyyyyy
Предполагая, что содержимое вашего файла было,
my_host,0.0.0.0 ssh-rsa xxxxxxxx
my_host1,1.2.3.4 ssh-rsa1 yyyyyyyy