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. 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; }}

will work fine for the code path 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   

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 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 ();   

On my system it will be printed on my computer:

  C: \ C: \ $ Recycle.Bin in the exception thread "main" java.io . UncheckIoExption: java.nio.file.AccessDeniedException: C: \ $ Recycle.Bin \ S-1-5-18   

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 files.walk , because you can never guarantee that you can go to the directory. Time will not be an error.

An important point to note is that 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.

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.

Future expansion

I believe it can still be fixed, though I do not know how 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

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -