0

I have to read a folder, count the number of files in the folder (can be of any type), display the number of files and then copy all the files to another folder (specified).

How would I proceed?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
user1759247
  • 101
  • 1
  • 5
  • 8
  • 4
    "I am student" means you should complete your homework yourself rather than giving it to your elder brother. As least you should give a try – Abubakkar Oct 24 '12 at 08:22
  • 2
    I would stop asking vague and unspecific questions, and after some research, I'd start coding. If and when I'm stuck, I'd post here [what I have tried](http://whathaveyoutried.com), and what error I can not get rid of, then with the help, I'd continue my work.... – ppeterka Oct 24 '12 at 08:23

3 Answers3

2

i Have to read a folder, count the number of files in the folder (can be of any type) display the number of files

You can find all of this functionality in the javadocs for java.io.File

and then copy all the files to another folder (specified)

This is a bit more tricky. Read: Java Tutorial > Reading, Writing and Creating of Files (note that the mechanisms described there are only available in Java 7 or later. If Java 7 is not an option, refer to one of many previous similar questions, e.g. this one: Fastest way to write to file? )

Community
  • 1
  • 1
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

you have all the sample code here :

http://www.exampledepot.com

http://www.exampledepot.com/egs/java.io/GetFiles.html

File dir = new File("directoryName");

String[] children = dir.list();
if (children == null) {
    // Either dir does not exist or is not a directory
} else {
    for (int i=0; i<children.length; i++) {
        // Get filename of file or directory
        String filename = children[i];
    }
}

// It is also possible to filter the list of returned files.
// This example does not return any files that start with `.'.
FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return !name.startsWith(".");
    }
};
children = dir.list(filter);


// The list of files can also be retrieved as File objects
File[] files = dir.listFiles();

// This filter only returns directories
FileFilter fileFilter = new FileFilter() {
    public boolean accept(File file) {
        return file.isDirectory();
    }
};
files = dir.listFiles(fileFilter);

The copying http://www.exampledepot.com/egs/java.io/CopyDir.html :

// Copies all files under srcDir to dstDir.
// If dstDir does not exist, it will be created.
public void copyDirectory(File srcDir, File dstDir) throws IOException {
    if (srcDir.isDirectory()) {
        if (!dstDir.exists()) {
            dstDir.mkdir();
        }

        String[] children = srcDir.list();
        for (int i=0; i<children.length; i++) {
            copyDirectory(new File(srcDir, children[i]),
                                 new File(dstDir, children[i]));
        }
    } else {
        // This method is implemented in Copying a File
        copyFile(srcDir, dstDir);
    }
}

However is very easy to gooole for this stuff :)

Cris
  • 4,947
  • 6
  • 44
  • 73
0

I know this is too late but below code worked for me. It basically iterates through each file in directory, if found file is a directory then it makes recursive call. It only gives files count in a directory.

public static int noOfFilesInDirectory(File directory) {
    int noOfFiles = 0;
    for (File file : directory.listFiles()) {
        if (file.isFile()) {
            noOfFiles++;
        }
        if (file.isDirectory()) {
            noOfFiles += noOfFilesInDirectory(file);
        }
    }
    return noOfFiles;
}
AndroidDev
  • 2,627
  • 6
  • 29
  • 41