java - Files.walk(), calculate total size -
I am trying to calculate the size of files on my disk in Java 7 as shown in my reply can be done.
However, if I want to use Java-8 streams, it will work for some folders, but not for everyone. will work fine for the code path I understand why it is coming from and how to avoid using files. Tablet filetree But how can this exception be avoided by using APIs? No, this exception can not be left. This is inside the idle flex of the lazy On my system it will be printed on my computer: and threw it on an (main) thread as an exception The third file goes on, at all that thread stop and firing. I believe this is a design failure, because now it is absolutely useless An important point to note is that If you give each wall, it can be pressed possibly King operation executed on its thread. But there is nothing that you are doing anyway. In addition, check that a file is actually accessible to useless (though somewhat useful), because you can not guarantee that it is after 1ms Can also be read. I believe it can still be fixed, though I do not know how Public static zero main (string [] args throws IOException {long size = files. Walk (path: ("C: /")). MapMong (MyMain :: count) .sum (); System.out.println ("size =" + size); } Fixed long counts (path path) {try files.size (path); } Grip (IOException | uncheckedioexpress e) {return 0; }}
a: / files / , but for the
c: / exception below this throw will
exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: c: \ $ Recycle.Bin \ S-java on 1-5-20. nio.file on JavakutilkIteratorkforEachRemaining (unknown source) at java.util.Spliterators $ IteratorSpliterator.forEachRemaining .FileTreeIterator.fetchNextIfNeeded on JavakniokfilekFileTreeIteratorkhasNext (unknown source) Java (unknown source) ( Unknown source) .atil.stream.AbstractPipeline.copyIn (unknown source at java.util.stream.AbstractPipeline.wrapAndCopyInto) at java.util.stream.ReduceOps (unknown En: rote) $ ReduceOp.evaluate unknown (unknown source JavaScript) Kprkashkviseshtyapeeplain. ((Unknown Source java.util.stream.LongPipeline.reduce unknown source)) java.util.stream.LongPipeline.sum (Unknown Source) (MyMain.java16) on MyMain.main
files.walk () , so why are not you seeing it fast and why there is no such way, consider this code Do:
longer size = files.walk (paths.get ("C: //") .peek (System.out :: println) .mapToLong (this: count). Sum ();
C: \ C: \ $ Recycle.Bin in the exception thread "main" java.io . UncheckIoExption: java.nio.file.AccessDeniedException: C: \ $ Recycle.Bin \ S-1-5-18
files.walk , because you can never guarantee that you can go to the directory. Time will not be an error.
sum () and
decrease () operations in stacktrace, because the path is sluggish Therefore, at the point of the
decrease () , the bulk of the stream machinery (which appears in stacktracks) is called, and then gets the path, at that point the
UncheckIOOxation occurs.
Future expansion
FileVisitOption s works perfectly is .
currently have a file Vijitivesnkfolelais S , if it is a file based on the premise, then I would suspect that
FileVisitOption.IGNORE_ON_IOEXCEPTION can be added However, we can not put that functionality properly there.
Comments
Post a Comment