find list of files and size for given windows path in java : useful to upload in shared folder location to save space in pc

 import java.io.IOException;

import java.nio.file.*;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.ArrayList;

import java.util.List;


public class FileLister {

    public static void main(String[] args) {

        String path = "C:\\Your\\Windows\\Path"; // Replace with the desired Windows path


        try {

            List<FileInformation> fileInformationList = listFilesAndSizes(path);

            for (FileInformation fileInfo : fileInformationList) {

                System.out.println(fileInfo);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


    static List<FileInformation> listFilesAndSizes(String path) throws IOException {

        List<FileInformation> fileInformationList = new ArrayList<>();


        Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {

            @Override

            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                String fileName = file.toString();

                long fileSize = attrs.size();

                fileInformationList.add(new FileInformation(fileName, fileSize));

                return FileVisitResult.CONTINUE;

            }


            @Override

            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {

                // Handle file visit failure here

                return FileVisitResult.CONTINUE;

            }

        });


        return fileInformationList;

    }


    static class FileInformation {

        private final String fileName;

        private final long fileSize;


        public FileInformation(String fileName, long fileSize) {

            this.fileName = fileName;

            this.fileSize = fileSize;

        }


        @Override

        public String toString() {

            return "File: " + fileName + " - Size: " + fileSize + " bytes";

        }

    }

}


------------
without sorted order 
i got below output




------------

import java.io.IOException;

import java.nio.file.*;

import java.nio.file.attribute.BasicFileAttributes;

import java.util.ArrayList;

import java.util.Comparator;

import java.util.List;


public class FileLister {

    public static void main(String[] args) {

        String path = "C:\\Your\\Windows\\Path"; // Replace with the desired Windows path


        try {

            List<FileInformation> fileInformationList = listFilesAndSizes(path);

            fileInformationList.sort(Comparator.comparingLong(FileInformation::getFileSize).reversed()); // Sort in descending order

            for (FileInformation fileInfo : fileInformationList) {

                System.out.println(fileInfo);

            }

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


    static List<FileInformation> listFilesAndSizes(String path) throws IOException {

        List<FileInformation> fileInformationList = new ArrayList<>();


        Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {

            @Override

            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {

                String fileName = file.toString();

                long fileSize = attrs.size();

                fileInformationList.add(new FileInformation(fileName, fileSize));

                return FileVisitResult.CONTINUE;

            }


            @Override

            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {

                // Handle file visit failure here

                return FileVisitResult.CONTINUE;

            }

        });


        return fileInformationList;

    }


    static class FileInformation {

        private final String fileName;

        private final long fileSize;


        public FileInformation(String fileName, long fileSize) {

            this.fileName = fileName;

            this.fileSize = fileSize;

        }


        public long getFileSize() {

            return fileSize;

        }


        @Override

        public String toString() {

            return "File: " + fileName + " - Size: " + fileSize + " bytes";

        }

    }

}

-----
output
example:


Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024