2

My requirement is to get the latest downloaded file from the given path. In My Project am downloading a file to "Downloads" folder, then I need to open the same edit and upload. I was able to download the file progrmmatically, Pls suggest how can i get the file name using Java or any Commands which can be implemented in Java.

am using Windows 7 OS

user1787641
  • 331
  • 2
  • 8
  • 25
  • 3
    So, to put things more clearly, you need to know the most recently created file in a given directory, right ? – SirDarius Dec 17 '13 at 09:07

2 Answers2

3

File contains lastModified().

    File uploadDirectory = new File("your_download_directory");
    File[] downloadedFiles = uploadDirectory.listFiles();

    Arrays.sort(downloadedFiles, new Comparator<File>() {
        @Override
        public int compare(File fileOne, File fileTwo) {
            return Long.valueOf(fileOne.lastModified()).compareTo(fileTwo.lastModified());
        }
    });

    for (File file : downloadedFiles) {
        if (file.isFile()) {
            // upload file
        }
    }
Big Bad Baerni
  • 996
  • 7
  • 21
1

This is only possible in Java >= 7.

Look at BasicFileAttributes.

http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/BasicFileAttributes.html

Also check out these links.

An example to get last access time of file in java using jdk1.7

Get the Last Access Time for a File

--- UPDATE BEGIN ---

To get all files in a folder use:

String dirName = "some-name";
File dir = new File(dirName);
File[] files = dir.listFiles();

See also: http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listFiles%28%29

--- UPDATE END ---

Community
  • 1
  • 1
peter.petrov
  • 38,363
  • 16
  • 94
  • 159
  • I need to get the name of the lastest downloaded file in hte directory.. From the above links am unable to get the same. – user1787641 Dec 17 '13 at 09:27
  • @user1787641 Get all the files but sort them by their creationTime, this was my point. What do you expect - to find a built-in method getLastCreatedFile in the API? – peter.petrov Dec 17 '13 at 09:29
  • I can sort based on the creation time, but how can i get the file name?? ya something similar to getLastCreatedFile(). – user1787641 Dec 17 '13 at 09:37