Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

The whole point of this code was to read json files through different directories in which they were inside a folder called "parm" and populate them in a csv file. Can you write a code to make it more efficent(shorter lines of code)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.util.Arrays;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;

public class start {

   public static void main(String[] args) throws FileNotFoundException {
              
      
//   C:\\Users\\AG05359\\Desktop\\bdf-rscz-etl\\streamsets
//       System.out.println(Arrays.toString(directories));
       String initDirectory = "C:\\Users\\AG05359\\Desktop\\bdf-rscz-etl\\streamsets";
       String [] directories = returnDirectories(initDirectory);
       System.out.println(Arrays.toString(directories));

       //17-19
       PrintWriter pw = new PrintWriter(new File("parm.csv"));
StringBuilder sb = new StringBuilder();
sb.append("Subject Name");
sb.append(',');
sb.append("Template Name");
sb.append(',');
sb.append("File Name");
sb.append(',');
sb.append("Property Name");
sb.append(',');
sb.append("Property Value");
sb.append('\n');
  
       for(int i =0;i<directories.length;i++){
//           System.out.println(directories[i]);
           String secondDirectory = "C:\\Users\\AG05359\\Desktop\\bdf-rscz-etl\\streamsets\\"+directories[i];
           String [] second_directories = returnDirectories(secondDirectory);
           System.out.println(Arrays.toString(second_directories));
           for(int j = 0; j<second_directories.length;j++){
               String thirdDirectory = "C:\\Users\\AG05359\\Desktop\\bdf-rscz-etl\\streamsets\\"+directories[i]+"\\"+second_directories[j];
               // Andy said why didnt i use some io method that will give me all the directoreis
               String [] third_directories = returnDirectories(thirdDirectory);
               System.out.println("third");
               System.out.println(Arrays.toString(third_directories));
               if(Arrays.asList(third_directories).contains("parm")){
                   System.out.println("true");
                   String [] parm_directories = returnJSONS("C:\\Users\\AG05359\\Desktop\\bdf-rscz-etl\\streamsets\\"+directories[i]+"\\"+second_directories[j]+"\\parm");
                   System.out.println(Arrays.toString(parm_directories));
                  
                   for(int k =0; k<parm_directories.length;k++){
                      
                       String[] keyValue = returnJsonValues("C:\\Users\\AG05359\\Desktop\\bdf-rscz-etl\\streamsets\\"+directories[i]+"\\"+second_directories[j]+"\\parm\\"+parm_directories[k]);
                       String key = keyValue[0];
                       String value = keyValue[1];
                       if( value.length() <= 4 ){
                          
                      
                      
               sb.append(directories[i]);
               sb.append(',');
               sb.append(second_directories[j]);
               sb.append(',');
               sb.append(parm_directories[k]);
               sb.append(',');
               sb.append(key);
               sb.append(',');
               sb.append(value);
               sb.append('\n');
                       }
                   }
               }
           }
       }
pw.write(sb.toString());
pw.close();
System.out.println("done!");
      
   }
  
   public static String[] returnDirectories(String initDirectory){
       File file = new File(initDirectory);
       String[] directories = file.list(new FilenameFilter() {
       @Override
       public boolean accept(File current, String name) {
       return new File(current, name).isDirectory();
       }
       });
      
       return directories;
   }
  
   public static String[] returnJSONS(String initDirectory){
       File f = new File(initDirectory);
       String[] matchingFiles = f.list(new FilenameFilter() {
       public boolean accept(File dir, String name) {
       return name.endsWith("json");
       }
       });
      
       String[] onlyFirstFile = {matchingFiles[0]};
       return matchingFiles; // return all json files in parm directory
//       return onlyFirstFile; // return only first json file of each parm directory
   }
  
   public static String[] returnJsonValues(String initDirectory) throws FileNotFoundException{
      
       JsonParser parser = new JsonParser();
       System.out.println("below");
       System.out.println(initDirectory);
       String key = "";
       String value = "";
       try{
           Object obj = parser.parse(new FileReader(initDirectory));
           JsonArray jsonArray = (JsonArray) obj;
          
   //       JsonObject jsonObject = jsonArray.get(0).getAsJsonObject().get("key").toString();
       key = jsonArray.get(0).getAsJsonObject().get("key").toString();
       value = jsonArray.get(0).getAsJsonObject().get("value").toString();
       System.out.println(jsonArray.get(0).getAsJsonObject().toString());
           System.out.println(key);
       System.out.println(value);
   } catch (Exception e) {
       key = "iinvalid jsonn";
       value = "iinvalid jsonn";
       System.out.println("invalid json");
       System.out.println("invalid json");
   }
       String [] keyValue = {key.substring(1, key.length()-1),value.substring(1, value.length()-1)};
       System.out.println(keyValue[0]);
       System.out.println(keyValue[1]);      
       return keyValue;
   }
  

}

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.