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();
}
}
}
}
}
}