How to check folder size or containing files in Java without knowing the file name with java.nio.file?

th_lo :

I had a similar question some time ago. Now I don't have any specific file name, because it is a preprocessor script. So I need to check the file size of a list of files or the size of a specific path.

the solution with the specific path was:

    var filePath = "//path//to//some//folder//file.name";
    try{
        var fileContent = java.nio.file.Files.size(java.nio.file.Paths.get(filePath));
        var fileSize = fileContent / 1048576;
        logger.debug("Filesize in MB: " + fileSize);
    }
    catch(e){
    ...

what I've already tried:

java.io.File(filePath).length();

here the size doesn't match what's actually in the folder.

and

java.nio.file.Files.list(filePath);

here I get a java.util.stream.ReferencePipeline$Head@... which I don't know how to read out in mirth

Maybe anyone could help or show me another solution to check the a file size before processing without reading in the file on Java Version "1.8.0._92" and Windows Server 2012R2 environment?

Cascader :

You can sum the sizes of all files within a directory using streams:


        long total = Files
           .list(Paths.get("c:/some/path/"))
           .mapToLong(p -> p.toFile().length())
           .sum();
        System.out.println(total);


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=198082&siteId=1