Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8257841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:20:14+00:00 2026-06-08T02:20:14+00:00

I am currently working on a basic file browser for android. I have a

  • 0

I am currently working on a basic file browser for android. I have a working version for copying files, however as it works its way through directories it copies the files it finds. I want to change that so that I can find the total size of all files before starting to copy, as to help with a better progress bar.

If there is another way to find the total size of a directory and all its contents?

Here is my current version. I am having trouble changing this, I have tried using an arrayList however when I try to copy the files at the end, I think they are trying to copy in the wrong order.

public void copyDirectory(File sourceLocation , File targetLocation) throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists() && !targetLocation.mkdirs()) {
                throw new IOException("Cannot create directory: " + targetLocation.getAbsolutePath());
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        } else {                
            File directory = targetLocation.getParentFile();
            if (directory != null && !directory.exists() && !directory.mkdirs()) {
                throw new IOException("Cannot create directory: " + directory.getAbsolutePath());
            }

            FileInputStream in = new FileInputStream(sourceLocation);
            FileOutputStream out = new FileOutputStream(targetLocation);

            long fileLength = sourceLocation.length();

            byte[] buf = new byte[1024];
            long total = 0;
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
                total += len;
                publishProgress((int) (total * 100 / fileLength));
            }
            in.close();
            out.close();
        }
    }

Solution

jtwigg’s answer should also work. I just thought I would add the solution I found. Can’t answer my own question so I will put it here.

Looping through all the files in the directory and keeping a running total seems to work. Although it requires looping first for the size and again to actually copy the files. Just call getDirectorySize() with the file or directory you wish to copy before calling copyDirectory().

private void getDirectorySize(File sourceLocation) throws IOException {
        if (sourceLocation.isDirectory()) {
            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                getDirectorySize(new File(sourceLocation, children[i]));
            }
        } else {
            totalFileSize += sourceLocation.length();
        }
}

The function will require the global long totalFileSize, and then all that is required is to replace:

publishProgress((int) (total * 100 / fileLength));

with:

publishProgress((int) (total * 100 / totalFileSize));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:20:17+00:00Added an answer on June 8, 2026 at 2:20 am

    If I understand you correctly, you wish to find the total size of all the files in the directory and then copy them.
    I would create another function, something like:

    public void beginCopy(File source, File destination)
    {
        ArrayList<PendingFile> filesToCopy = new ArrayList<PendingFile>();
        long totalSize = copyDirectory(source, destination, filesToCopy);
        // totalsize now contains the size of all the files
        // files to copy now contains a list of source and destination files
    
        // now modifying your copy method we can copy all the files
        long totalThusFar = 0;
        for (PendingFile pending : filesToCopy)
        {
            FileInputStream in = new FileInputStream(pending.source);
            FileOutputStream out = new FileOutputStream(pending.destination);
    
            long fileLength = sourceLocation.length();
    
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
                totalThusFar += len;
                publishProgress((int) (total * 100 / totalsize));
            }
            in.close();
            out.close();
        }
    }
    

    you would need a PendingFile class/structure just to hold both source and destinations. You will add them to the ArrayList in your copy method like this:

    public long copyDirectory(File sourceLocation , File targetLocation, ArrayList list) throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists() && !targetLocation.mkdirs()) {
                throw new IOException("Cannot create directory: " + targetLocation.getAbsolutePath());
            }
    
            String[] children = sourceLocation.list();
            long totalSize = 0;
            for (int i = 0; i < children.length; i++) {
                totalSize += copyDirectory(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]), list);
                return totalSize;
            }
        } else {                
            File directory = targetLocation.getParentFile();
            if (directory != null && !directory.exists() && !directory.mkdirs()) {
                throw new IOException("Cannot create directory: " + directory.getAbsolutePath());
            }
    
            list.add(new PendingFile(sourceLocation, targetLocation));
            return sourceLocation.length;
        }
    }
    

    I wrote all this just now so It probably won’t work straight away but I think you should be able to get it working with this. Goodluck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an MVC 3 site that is working and currently quite basic. There
I'm currently working on an AJAX file upload script, which works like a charm
Currently working on a wp7 App, its quite basic. the user has a counter
I'm currently working on a (relatively) basic web application that functions as a time
This may seem a basic question but my is not currently working. I am
I'm currently working on an SSIS package to load mainframe logs from multiple server/file
I'm currently working on a project where we have a large data warehouse which
I'm occurring some trouble writing a basic webserver in JAVA. It's currently working fine
I am currently working on a website that users can download binary media files
currently I am working on a spring based application. I do have some unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.