0

I would like to merge the following .properties files into one. So for example I have structure:

IAS
├── default
│   ├── gateway.properties
│   └── pennytunnelservice.properties
└── FAT
    ├── gateway.properties
    ├── IAS-jer-1
    │   └── gateway.properties
    └── pennytunnelservice.properties

My goal is to have two merged files (in this example) pennytunnelservice.properties nad gateway.properties.

In default/gateway.properties is for example:

abc=$change.me
def=test

In FAT/gateway.properties is for example:

abc=123
ghi=234

In FAT/pennytunnelservice.properties is for example:

mno=text

In FAT/IAS-jer-1/gateway.properties is for example:

xyz=123
ghi=98

And the result should be two files with these rows:

pennytunnelservice.properties

mno=text

gateway.properties

abc=123
def=test
ghi=98
xyz=123

Do you have any idea how to do that?


UPDATED !!!

I have written something like this:

    public static void main(String[] args) throws IOException {

    String dirName = "/IAS";
    File file = new File(dirName);
    Map<String,Properties> files = new HashMap<>();


    Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            System.out.println(file);

            Properties prop = new Properties();
            FileInputStream input = new FileInputStream(file.toString());

            prop.load(input);
            files.put(file.getFileName().toString(), prop);

            return FileVisitResult.CONTINUE;
        }
    });

And the result is

{pennytunnelservice.properties={mno=text}, gateway.properties={abc=123, ghi=234}}

The problem is that files are loaded in the wrong way/order:

IAS/default/pennytunnelservice.properties
IAS/default/gateway.properties
IAS/FAT/IAS-jer-1/gateway.properties
IAS/FAT/pennytunnelservice.properties
IAS/FAT/gateway.properties

It should be:

IAS/default/pennytunnelservice.properties
IAS/default/gateway.properties
IAS/FAT/pennytunnelservice.properties
IAS/FAT/gateway.properties
IAS/FAT/IAS-jer-1/gateway.properties
Michal Sipek
  • 536
  • 1
  • 6
  • 23
  • I suggest following approach. Keep seperate List one for gateway.properties and one other pennytunnelservice.properties. Parse each file and add those to correct List. Finally, keep adding those List of Properties to a single property object. Then iterate over that final Properties object and extract key value pairs and write to a file. – akshaya pandey Jan 23 '18 at 08:43
  • 1
    Have a look here, and you might see how to do it: https://stackoverflow.com/questions/2004833/how-to-merge-two-java-util-properties-objects . What you will have to do is load each `Properties` with your files' content – DamCx Jan 23 '18 at 08:45
  • 1
    From Javadoc "The file tree is traversed [depth-first](https://docs.oracle.com/javase/9/docs/api/java/nio/file/Files.html#walkFileTree-java.nio.file.Path-java.util.Set-int-java.nio.file.FileVisitor-)", so you need to store `Paths` in a `Collection` and sort, then process the files. – Venkata Raju Jan 24 '18 at 02:21
  • @VenkataRaju thanks, it's a good point. – Michal Sipek Jan 24 '18 at 08:28

1 Answers1

1

You need a Map<String, Properties>. is the .properties filename and is the content read from the file.

Recursively, using a FileVisitor look for property files.

For each file found, load it updating the old content in the map if the same file name has already been found.

When all files have been processed iterate over all filenames (keys in map) and for each one save a new properties file with the content gathered from all found files.

Daniel Voina
  • 3,185
  • 1
  • 27
  • 32
  • Thank you, could you check my last edit? I tried to implement one solution, but have problem - files are loaded in the wrong way/order. Could you give me a small hint? – Michal Sipek Jan 23 '18 at 09:57
  • @sipekmichal.cz: the order is givn by the graph traversal algorithm. You have to make another FileVisitor (probably derived from SimpleFileVisitor) that handles the order in the way you want (alphabetically, case insensitive, ...). In your case it seems that you want to treat the directories last. – Daniel Voina Jan 23 '18 at 10:26
  • See this for more details: https://stackoverflow.com/questions/10396649/order-of-traversal-in-files-walkfiletree – Daniel Voina Jan 23 '18 at 10:37