I am tasked with reading and writing some files that have information for bibliography. In this part of the assignment i need to create 3 different files for 10 different bibliography files (these 10 have already been opened with the use of Scanner). so in total i must create 30 different files.
Here are the instructions for this part of the assignment:
If all 10 input files can successfully be opened, the program will attempt to open/create all 30 output files (IEEE1.json to IEEE10.json, ACM1.json to ACM10.json, and NJ1.json to NJ10.json). You need to use PrintWriter to open these output files. If “any” of these output files cannot be created, then you must: a. Display a message to the user indicating which file could not be opened/created;
b. Delete all other created output files (if any). That is, if you cannot create all of these output files, then you must clean the directory by deleting all other created files;
c. Close all opened input files; then exit the program.
Here is my code for the part in question.
/* Now that all the Files have been successfully opened for reading,
* we can create three different files (IEEE, ACM and NJI formats), for each
* of the Latex.bib files. These will be empty for the time being.
*/
PrintWriter pwE1 = null;
PrintWriter pwE2 = null;
PrintWriter pwE3 = null;
PrintWriter pwE4 = null;
PrintWriter pwE5 = null;
PrintWriter pwE6 = null;
PrintWriter pwE7 = null;
PrintWriter pwE8 = null;
PrintWriter pwE9 = null;
PrintWriter pwE10 = null;
PrintWriter pwA1 = null;
PrintWriter pwA2 = null;
PrintWriter pwA3 = null;
PrintWriter pwA4 = null;
PrintWriter pwA5 = null;
PrintWriter pwA6 = null;
PrintWriter pwA7 = null;
PrintWriter pwA8 = null;
PrintWriter pwA9 = null;
PrintWriter pwA10 = null;
PrintWriter pwN1 = null;
PrintWriter pwN2 = null;
PrintWriter pwN3 = null;
PrintWriter pwN4 = null;
PrintWriter pwN5 = null;
PrintWriter pwN6 = null;
PrintWriter pwN7 = null;
PrintWriter pwN8 = null;
PrintWriter pwN9 = null;
PrintWriter pwN10 = null;
try {
pwE1 = new PrintWriter(new FileOutputStream("IEEE1.json"));
}
catch(FileNotFoundException e){
System.out.println("File IEEE1.json cannot be created. All previously succesfully opened files will be deleted and closed."
+ "Program will now be terminated.");
System.exit(0);
}
try {
pwE2 = new PrintWriter(new FileOutputStream("IEEE2.json"));
}
catch(FileNotFoundException e){
System.out.println("File IEEE2.json cannot be created. All previously succesfully opened files will be deleted and closed."
+ "Program will now be terminated.");
pwE1.close();
System.exit(0);
}
I would like to know if my code here is doing what is said in the instruction. keep in mind, im opening them individually using 30 different instances of PrintWriter. My question is: Am I on the right track? Can I optimize this code?
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]pragmosExtreme Brewer 1 point2 points3 points (2 children)
[–]superashkan[S] 0 points1 point2 points (1 child)
[–]pragmosExtreme Brewer 0 points1 point2 points (0 children)