-1

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 it's complaining about, but I'm not sure how to feed in the right parameters. 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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
New2Python
  • 325
  • 1
  • 4
  • 17
  • Does this answer your question? [Get java.nio.file.Path object from java.io.File](https://stackoverflow.com/questions/15512200/get-java-nio-file-path-object-from-java-io-file) – f1sh Jan 05 '22 at 21:49
  • 1
    Try using [file.toURI()](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#toURI()). – Alias Cartellano Jan 05 '22 at 21:53
  • thank you for the quick replies. wasnt sure what to do with the java.nio.file.Path comment, Im still pretty new to java. I did get it working by doing the following: – New2Python Jan 05 '22 at 22:16
  • thank you for the quick replies, wasnt sure what to do exactly with the java.nio.file.Path answer, Im still new to Java. This did work, but I had to change the type in my class: serverCfgParser.parseFile(configFile.toURI()); – New2Python Jan 05 '22 at 22:18

1 Answers1

0

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());
}
Ty Viera
  • 28
  • 2
  • thanks! this also works but IDE me add a "throws IOException" to public static void main which I dont get....or to surround with try catch which makes sense. thanks! – New2Python Jan 05 '22 at 22:43