@@ -218,15 +218,16 @@ public static Boolean isValidNumericValue(Float value, ConceptNumeric concept) {
218218 */
219219 public static String getFileAsString (File file ) throws IOException {
220220 StringBuilder fileData = new StringBuilder (1000 );
221- BufferedReader reader = new BufferedReader (new InputStreamReader (new FileInputStream (file ), StandardCharsets .UTF_8 ));
222- char [] buf = new char [1024 ];
223- int numRead ;
224- while ((numRead = reader .read (buf )) != -1 ) {
225- String readData = String .valueOf (buf , 0 , numRead );
226- fileData .append (readData );
227- buf = new char [1024 ];
228- }
229- reader .close ();
221+ try (BufferedReader reader = new BufferedReader (
222+ new InputStreamReader (new FileInputStream (file ), StandardCharsets .UTF_8 ))) {
223+ char [] buf = new char [1024 ];
224+ int numRead ;
225+ while ((numRead = reader .read (buf )) != -1 ) {
226+ String readData = String .valueOf (buf , 0 , numRead );
227+ fileData .append (readData );
228+ buf = new char [1024 ];
229+ }
230+ }
230231 return fileData .toString ();
231232 }
232233
@@ -1115,9 +1116,7 @@ public static File getDirectoryInApplicationDataDirectory(String folderName) thr
11151116 * @param outFile file pointer to the location the xml file is to be saved to
11161117 */
11171118 public static void saveDocument (Document doc , File outFile ) {
1118- OutputStream outStream = null ;
1119- try {
1120- outStream = new FileOutputStream (outFile );
1119+ try (OutputStream outStream = new FileOutputStream (outFile )) {
11211120 TransformerFactory tFactory = TransformerFactory .newInstance ();
11221121 Transformer transformer = tFactory .newTransformer ();
11231122 transformer .setOutputProperty (OutputKeys .INDENT , "yes" );
@@ -1135,14 +1134,8 @@ public static void saveDocument(Document doc, File outFile) {
11351134 throw new ModuleException ("Error while saving dwrmodulexml back to dwr-modules.xml" , e );
11361135 } catch (FileNotFoundException e ) {
11371136 throw new ModuleException (outFile .getAbsolutePath () + " file doesn't exist." , e );
1138- } finally {
1139- try {
1140- if (outStream != null ) {
1141- outStream .close ();
1142- }
1143- } catch (Exception e ) {
1144- log .warn ("Unable to close outstream" , e );
1145- }
1137+ } catch (IOException e ) {
1138+ log .warn ("Unable to close outstream" , e );
11461139 }
11471140 }
11481141
@@ -1563,20 +1556,10 @@ public static String generateUid() {
15631556 * @param comment
15641557 */
15651558 public static void storeProperties (Properties properties , File file , String comment ) {
1566- OutputStream outStream = null ;
1567- try {
1568- outStream = new FileOutputStream (file , true );
1559+ try (OutputStream outStream = new FileOutputStream (file , true )) {
15691560 storeProperties (properties , outStream , comment );
15701561 } catch (IOException ex ) {
15711562 log .error ("Unable to create file " + file .getAbsolutePath () + " in storeProperties routine." );
1572- } finally {
1573- try {
1574- if (outStream != null ) {
1575- outStream .close ();
1576- }
1577- } catch (IOException ioe ) {
1578- // pass
1579- }
15801563 }
15811564 }
15821565
@@ -1614,24 +1597,14 @@ public static void storeProperties(Properties properties, OutputStream outStream
16141597 * @param inputStream the input stream to read from
16151598 */
16161599 public static void loadProperties (Properties props , InputStream inputStream ) {
1617- InputStreamReader reader = null ;
1618- try {
1619- reader = new InputStreamReader (inputStream , StandardCharsets .UTF_8 );
1600+ try (InputStreamReader reader = new InputStreamReader (inputStream , StandardCharsets .UTF_8 )) {
16201601 props .load (reader );
16211602 } catch (FileNotFoundException fnfe ) {
16221603 log .error ("Unable to find properties file" + fnfe );
16231604 } catch (UnsupportedEncodingException uee ) {
16241605 log .error ("Unsupported encoding used in properties file" + uee );
16251606 } catch (IOException ioe ) {
16261607 log .error ("Unable to read properties from properties file" + ioe );
1627- } finally {
1628- try {
1629- if (reader != null ) {
1630- reader .close ();
1631- }
1632- } catch (IOException ioe ) {
1633- log .error ("Unable to close properties file " + ioe );
1634- }
16351608 }
16361609 }
16371610
0 commit comments