1

I want to merge some files that look like this:

File 1
line 01
line 02
line 03

File2
line 04
line 05

and the output should be like this:

NewFile
line 01
line 02
line 03
line 04
line 05

My algorithm receives the URL of a local directory and then iterates through it to get only those files with json extensions and to merge them. For example in this URL there are two json files but the last one is attached to the new file.

This is my code so far

public class ReadFiles {
    static FileWriter fileWriter;
    public static void main(String... args) throws IOException, InterruptedException, ParseException {
        File[] files = new File("C:\\xampp\\htdocs\\project-js\\blocks\\actions\\Application").listFiles();
        showFiles(files);
    }

    public static void showFiles(File[] files) throws FileNotFoundException, IOException, ParseException {
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("Directory: " + file.getName());
                showFiles(file.listFiles());

            } else {
                if (file.getName().endsWith("json")) { // find only those with json extensions
                    System.out.println(file.getName());

                    JSONParser parser = new JSONParser();

                    fileWriter = new FileWriter("C:\\xampp\\htdocs\\project-js\\blocks\\actions\\Application\\ApplicationDesc.js");
                    fileWriter.write(parser.parse(new FileReader(file)).toString());
                    fileWriter.close();

                    try {
                        System.out.println(parser.parse(new FileReader(file)));
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }  
            }
        }
    }
}

3 Answers3

0

You need simply to append the content of the second file to the end of the first file.

Note that a json content file merged with another json content file result in a non json content file.

For example a.json:

{ "name":"John" }

and b.json:

{ "name":"Anna" }

result in a combined.json

{ "name":"John" }
{ "name":"Anna" }

that is not a valid json.

This happens also if you have an array of json objects:

a.json:

[{ "name":"John" }, { "name":"Anna" }] 

b.json

[{ "name":"Keith"}]

and the combination of them:

[{ "name":"John" }, { "name":"Anna" }] 
[{ "name":"Keith"}]

that is not a valid json content file

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

The problem in that code:

  fileWriter = new FileWriter("C:\\xampp\\htdocs\\project-js\\blocks\\actions\\Application\\ApplicationDesc.js");
  fileWriter.write(parser.parse(new FileReader(file)).toString());
  fileWriter.close();

Is that it creates a new FileWriter every time and overwrites the previous file. You can either create the fileWriter with:

fileWriter = new FileWriter("C:\\xampp\\htdocs\\project-js\\blocks\\actions\\Application\\ApplicationDesc.js",true); //The true in the end is to append to file

Or create the fileWriter outside the loop and then close it when the loop finishes. If the files have contents like in your example then you will be fine. If they are JSON files and you expect a valid json result then you cannot just append them to one big file

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
0

You can collect the text of all the files.

For that you need to read each file one by one and extract it's content

Use this to extract the contents of a single file as String

private static String readFileAsString(String filePath){
    StringBuilder contentBuilder = new StringBuilder();

    try (Stream<String> stream = Files.lines( Paths.get(filePath), StandardCharsets.UTF_8)){
        stream.forEach(s -> contentBuilder.append(s).append("\n"));
    }
    catch (IOException e){
        e.printStackTrace();
    }

    return contentBuilder.toString();
}

You can create a JSONObject (org.json) for each extracted String

JSONObject jsonObj = new JSONObject(extractedString);

then store jsonObj it in a List.

To use JSONObject add this to your pom.xml or download jars manually

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>

Merge JSONObjects from the List by using the method below

public static JSONObject mergeJSONObjects(JSONObject json1, JSONObject json2) {
    JSONObject mergedJSON = new JSONObject();
    try {
        mergedJSON = new JSONObject(json1, JSONObject.getNames(json1));
        for (String key : JSONObject.getNames(json2)) {
            mergedJSON.put(key, json2.get(key));
        }

    } catch (JSONException e) {
        throw new RuntimeException("JSON Exception" + e);
    }
    return mergedJSON;
}

Then write the final String in a file.

Use something like this

 BufferedWriter writer = new BufferedWriter(new FileWriter("test.txt"));
 writer.write();
 writer.close();

This works but the size of the files should be small, for bigger files you need to find a better solution.

Thanos M
  • 604
  • 6
  • 21