简体   繁体   中英

list of file names sort to a specific order

My java code lists all code files under a directory of file system, and load each file one by one:

File[] files = mDir.listFiles();
for(File f:  files) {
   System.out.println(f.getPath());
   //load code file
   System.load(f);
}

The above code logically looks good, but is not suitable for my case.

My case is that I can NOT load them in a loop one by one, because there are dependencies among those code files. I need to load the files in a specific order according to dependencies.

Say, I already know there are following files under the directory mDir which should be load in the following order:

["dFile", "xFile", "aFile", "hFile"]

and I already got the directory instance mDir .

How can I load files with above order efficiently in java?

If you already know which files you are interested in then just load them in the proper order.

If you have to see which files are available first and then load them in the specific order, then use one loop to get the names of the existing files, then process the list by picking the correct files in the correct order.

I'd suggest just setting the working directory correctly (see Changing the current working directory in Java? ) and then doing

for(String fname : fileArray) {
    System.load(new File(fname));
}

(where fileArray is the list of file names) or

for(String fname : fileArray) {
    System.load(new File(mDir.getPath() + fname));
}

if you're intent on loading from a specific directory.

Other than that, you'd need to divine the dependencies from each file in order, or read the list of files to load from some other source (an array, another file, whatever).

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