简体   繁体   中英

Java - How to list files on mapped network drive?

Im trying to connect to a mapped drive (sharepoint) to make a list of the files that exists.

So, I have this code that works fine on listing the files on my local PC:

public static void main(String[] args) {
    // Directory path here
    String path = "/"; 

    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles(); 

    for(int i = 0; i < listOfFiles.length; i++){
        if(listOfFiles[i].isFile()){
            files = listOfFiles[i].getName();
            System.out.println(files);
        }
    }
}

When path = "/", it displays all the files on my local drive C:. Now I would like to know if there is a way to adapt this to list the files of a mapped network drive (mapped as Y: for example).

if your os is windows you can use the \\\\Server\\shared_folder

public static void main(String[] args) {
    // Directory path here
    String path = "\\\\server\\shared_folder"; 

    String files;
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles(); 

    for(int i = 0; i < listOfFiles.length; i++){
        if(listOfFiles[i].isFile()){
            files = listOfFiles[i].getName();
            System.out.println(files);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM