0

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?

user842225
  • 5,445
  • 15
  • 69
  • 119

2 Answers2

0

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.

Mark Wagoner
  • 1,729
  • 1
  • 13
  • 20
  • Hi, yes, I have the same mind as your words, but how to pick the correct files in correct order in java code? I don't want to have multiple lines of codes all doing System.load(xxx), I am seeking for a way to sort the files in proper order then load them by a for loop, if it is possible. Or some elegant/efficient way than writing System.load(xxx) file by file – user842225 Apr 25 '14 at 12:00
0

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).

Community
  • 1
  • 1