I have a functioning class serverCfgParser
that works perfectly for a single file:
serverCfgParser.parseFile("src/main/resources/static/server_dev_devops_pc.cfg");
I need to change the logic so that multiple files ending with.cfg get processed. I want to pass in a variable instead of a hard coded path. Ill need to loop all files in a given directory and pass those onto serverCfgParser. I have looked at some examples but can get this to work.
File serverConfigs = new File("src/main/resources/static");
File[] files = serverConfigs.listFiles((d, name) -> name.endsWith(".cfg"));
for (File configFile : files) {
System.out.println(configFile);
}
ServerCfgParser serverCfgParser = new ServerCfgParser();
for (File configFile : files) {
Charset encoding = Charset.defaultCharset();
List<String> lines = Files.readAllLines(Paths.get(configFile), encoding);
serverCfgParser.parseFile(configFile);
}
I used the first for loop to prove to test that file paths were being populated correctly.
src\main\resources\static\server_dev_aws_app1.cfg
src\main\resources\static\server_dev_aws_app2.cfg
src\main\resources\static\server_dev_aws_app3.cfg
src\main\resources\static\server_dev_aws_app4.cfg
src\main\resources\static\server_dev_aws_app5.cfg
src\main\resources\static\server_dev_aws_app6.cfg
src\main\resources\static\server_dev_devops_app1.cfg
src\main\resources\static\server_dev_devops_app2.cfg
src\main\resources\static\server_dev_devops_app3.cfg
src\main\resources\static\server_dev_devops_app4.cfg
src\main\resources\static\server_dev_devops_app5.cfg
src\main\resources\static\server_dev_devops_app6.cfg
src\main\resources\static\server_dev_mansible_app5.cfg
src\main\resources\static\server_dev_mansible_app6.cfg
src\main\resources\static\server_test_mansible_app5.cfg
src\main\resources\static\server_test_mansible_app6.cfg
JAVA is complaining about Paths.get(configFile)
:
The method get(URI) in the type Paths is not applicable for the arguments (File)
I get what its complaining about, but not sure how to feed in the right params. Every example I looked at is as above but for single files only, I have yet to find an example of looping through multiple files.
Help is greatly appreciated.
You could use the toPath method of the File class:
Charset encoding = Charset.defaultCharset();
for (File configFile : files) {
List<String> lines = Files.readAllLines(configFile.toPath(), encoding);
serverCfgParser.parseFile(configFile.toString());
}
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.