英文:
Java get size of event.getDragboard().getFiles().stream().map(File::length).toString());
问题
我想要获取我的文件大小:
event.getDragboard().getFiles().stream().map(File::getName).collect(Collectors.joining("\n")),
event.getDragboard().getFiles().stream().map(File::getPath).collect(Collectors.joining("\n")),
event.getDragboard().getFiles().stream().map(file -> Long.toString(file.length())).collect(Collectors.joining("\n")));
问题实际上是我正确获取了名称和路径,但没有获取大小:
java.util.stream.ReferencePipeline$3@5b41d7e4
英文:
I woudlike to get size of my file :
event.getDragboard().getFiles().stream().map(File::getName).collect(Collectors.joining("\n")),
event.getDragboard().getFiles().stream().map(File::getPath).collect(Collectors.joining("\n")),
event.getDragboard().getFiles().stream().map(File::length).toString());
The problem actually I get correct Name and path but not size:
java.util.stream.ReferencePipeline$3@5b41d7e4
答案1
得分: 1
By calling:
event.getDragboard().getFiles().stream().map(File::length).toString());
you are converting Stream<Long> into String.
What you want actually is to convert every Long in the stream into String and then join them as you did with path or name.
How to convert Stream<Long> into String?
getFiles().stream()
.map(File::length) // Stream<Long>
.map(String::valueOf) // Stream<String>
.collect(Collectors.joining("\n")); // String
The function String.valueOf() takes a Long and returns its String representation, so we can use this function for conversion from Long to String.
How to convert Stream<Long> into Long?
In case you would like to get sum [1] of all Longs in the stream, you can use the sum function of LongStream.
getFiles().stream()
.map(File::length) // Stream<Long>
.mapToLong(Long::longValue) // LongStream
.sum(); // Long
By calling mapToLong, we have converted Stream<Long> into LongStream.
[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/LongStream.html#sum()
英文:
By calling:
event.getDragboard().getFiles().stream().map(File::length).toString());
you are converting Stream<Long> into String.
What you want actually is to convert every Long in the stream into String and then join them as you did with path or name.
How to convert Stream<Long> into String?
getFiles().stream()
.map(File::length) // Stream<Long>
.map(String::valueOf) // Stream<String>
.collect(Collectors.joining("\n")); // String
The function String.valueOf() takes a Long and returns it's String representation, so we can use this function for conversion from Long to String.
How to convert Stream<Long> into Long?
In case you would like to get sum [1] of all Longs in the stream, you can use sum function of LongStream.
getFiles().stream()
.map(File::length) // Stream<Long>
.mapToLong(Long::longValue) // LongStream
.sum(); // Long
By calling mapToLong we have converted Stream<Long> into LongStream.
[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/LongStream.html#sum()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论