I am trying to read a simple text file that looks like this:
operationName1 Value
operationName2 Value
The operation names are strings (there are only two possible operations) and the values are ints.
My goal is to have an array as the output that looks like this:
[[1,10],[2,5]]
In the specified array, I would transform the operationName1 into 1 and operationName2 into 2, with the corresponding value.
This is what I tried:
try{
Path path = Paths.get("./myFile.txt");
String[][] array = Files.lines(path)
.map(s -> s.split("\\s+", 2))
.map(a -> new String[]{a[0], a[1]})
.toArray(String[][]::new);
System.out.println(Arrays.toString(array));
}catch(IOException e){
}
I did not change the names to integer in this code, I simply started by trying to get an array of string that I can print to see if I am reading the file correctly.
My output looks like this:
[[Ljava.lang.String;@682a0b20, [Ljava.lang.String;@3d075dc0, [Ljava.lang.String;@214c265e, [Ljava.lang.String;@448139f0, [L ...
Am I only getting a stream instead of strings?
The path is correct, I can read the file normally using standard syntax. But I would really like to understand the new syntax to start on the right foot, as understanding it seems helpful.
My question: Would you have an idea on how can I correct this problem, or even better, an idea on how I could have my expected output using the most recent syntax? (Java 8 if possible?)
PS: I also saw this syntax but I am not sure how to complete it to get my result, and I am not sure if this would be better or worse (but it seems to be Java 8).
Files.lines(Paths.get(ClassLoader.getSystemResource("myFile.txt")
.toURI())).forEach(System.out::println);