I am trying to understand the Java 8 Stream-feature.
I would like to catch all files in an array (could als be list) in all subdirectories of a specific directory path.
My code throws an java.lang.ArrayStoreException Error
File[] files = Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .toArray(File[]::new);
Another thing I tried is just add the file names to an existing ArrayList:
ArrayList<String> existingNames = new ArrayList<String>(); Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .forEach(p -> existingNames.add(p.getFileName()));
This throws a java.lang.ArrayStoreException Error as well.
Something else I tried with the same result was:
File[] files = Files.walk(Paths.get(path)) .filter(Files::isRegularFile) .toArray(File[]::new);
Can anyone point me in the right direction?
The full stack trace is:
Exception in thread "main" java.lang.ArrayStoreException at java.lang.System.arraycopy(Native Method) at java.util.stream.SpinedBuffer.copyInto(SpinedBuffer.java:198) at java.util.stream.Nodes$SpinedNodeBuilder.copyInto(Nodes.java:1290) at java.util.stream.SpinedBuffer.asArray(SpinedBuffer.java:215) at java.util.stream.Nodes$SpinedNodeBuilder.asArray(Nodes.java:1296) at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:439) at nachbearbeitung.CheckExistingMAIDs.main(CheckExistingMAIDs.java:41)
Thanks in advance!