Java获取`event.getDragboard().getFiles().stream().map(File::length).toString()`的大小。

huangapple go评论74阅读模式
英文:

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&lt;Long&gt; 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&lt;Long&gt; into String?

getFiles().stream()
          .map(File::length) // Stream&lt;Long&gt;
          .map(String::valueOf) // Stream&lt;String&gt;
          .collect(Collectors.joining(&quot;\n&quot;)); // 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&lt;Long&gt; 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&lt;Long&gt;
          .mapToLong(Long::longValue) // LongStream
          .sum(); // Long

By calling mapToLong we have converted Stream&lt;Long&gt; into LongStream.


[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/stream/LongStream.html#sum()

huangapple
  • 本文由 发表于 2020年9月15日 17:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63898539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定