diff options
author | Dominik Stadler <centic@apache.org> | 2019-12-15 14:53:11 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2019-12-15 14:53:11 +0000 |
commit | 1a5cec89d8d36f2a7cd5643b221befd57fa515b7 (patch) | |
tree | 6a6719f33761c08a719599393ca310eafcb7834e /src/examples | |
parent | 2748844549b50dc11e8ef19dcd506c820c1a1e19 (diff) | |
download | poi-1a5cec89d8d36f2a7cd5643b221befd57fa515b7.tar.gz poi-1a5cec89d8d36f2a7cd5643b221befd57fa515b7.zip |
Replace manual close with try-with-resources
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1871590 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/examples')
-rw-r--r-- | src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java | 118 | ||||
-rw-r--r-- | src/examples/src/org/apache/poi/hssf/usermodel/examples/OfficeDrawing.java | 21 |
2 files changed, 58 insertions, 81 deletions
diff --git a/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java b/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java index 6c097df106..83d4701289 100644 --- a/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java +++ b/src/examples/src/org/apache/poi/hsmf/examples/Msg2txt.java @@ -63,62 +63,56 @@ public class Msg2txt { public void processMessage() throws IOException { String txtFileName = fileNameStem + ".txt"; String attDirName = fileNameStem + "-att"; - PrintWriter txtOut = null; - try { - txtOut = new PrintWriter(txtFileName); - try { - String displayFrom = msg.getDisplayFrom(); - txtOut.println("From: "+displayFrom); - } catch (ChunkNotFoundException e) { - // ignore - } - try { - String displayTo = msg.getDisplayTo(); - txtOut.println("To: "+displayTo); - } catch (ChunkNotFoundException e) { - // ignore - } - try { - String displayCC = msg.getDisplayCC(); - txtOut.println("CC: "+displayCC); - } catch (ChunkNotFoundException e) { - // ignore - } - try { - String displayBCC = msg.getDisplayBCC(); - txtOut.println("BCC: "+displayBCC); - } catch (ChunkNotFoundException e) { - // ignore - } - try { - String subject = msg.getSubject(); - txtOut.println("Subject: "+subject); - } catch (ChunkNotFoundException e) { - // ignore - } - try { - String body = msg.getTextBody(); - txtOut.println(body); - } catch (ChunkNotFoundException e) { - System.err.println("No message body"); - } - - AttachmentChunks[] attachments = msg.getAttachmentFiles(); - if(attachments.length > 0) { - File d = new File(attDirName); - if(d.mkdir()) { - for(AttachmentChunks attachment : attachments) { - processAttachment(attachment, d); - } - } else { - System.err.println("Can't create directory "+attDirName); - } - } - } finally { - if(txtOut != null) { - txtOut.close(); - } - } + try (PrintWriter txtOut = new PrintWriter(txtFileName)) { + try { + String displayFrom = msg.getDisplayFrom(); + txtOut.println("From: " + displayFrom); + } catch (ChunkNotFoundException e) { + // ignore + } + try { + String displayTo = msg.getDisplayTo(); + txtOut.println("To: " + displayTo); + } catch (ChunkNotFoundException e) { + // ignore + } + try { + String displayCC = msg.getDisplayCC(); + txtOut.println("CC: " + displayCC); + } catch (ChunkNotFoundException e) { + // ignore + } + try { + String displayBCC = msg.getDisplayBCC(); + txtOut.println("BCC: " + displayBCC); + } catch (ChunkNotFoundException e) { + // ignore + } + try { + String subject = msg.getSubject(); + txtOut.println("Subject: " + subject); + } catch (ChunkNotFoundException e) { + // ignore + } + try { + String body = msg.getTextBody(); + txtOut.println(body); + } catch (ChunkNotFoundException e) { + System.err.println("No message body"); + } + + AttachmentChunks[] attachments = msg.getAttachmentFiles(); + if (attachments.length > 0) { + File d = new File(attDirName); + if (d.mkdir()) { + for (AttachmentChunks attachment : attachments) { + processAttachment(attachment, d); + } + } else { + System.err.println("Can't create directory " + attDirName); + } + } + } } /** @@ -137,15 +131,9 @@ public class Msg2txt { } File f = new File(dir, fileName); - OutputStream fileOut = null; - try { - fileOut = new FileOutputStream(f); - fileOut.write(attachment.getAttachData().getValue()); - } finally { - if(fileOut != null) { - fileOut.close(); - } - } + try (OutputStream fileOut = new FileOutputStream(f)) { + fileOut.write(attachment.getAttachData().getValue()); + } } /** diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/OfficeDrawing.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/OfficeDrawing.java index ce4935230c..39b388ea32 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/OfficeDrawing.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/OfficeDrawing.java @@ -173,23 +173,12 @@ public class OfficeDrawing { private static int loadPicture( String path, HSSFWorkbook wb ) throws IOException { int pictureIndex; - FileInputStream fis = null; - ByteArrayOutputStream bos = null; - try - { - fis = new FileInputStream( path); - bos = new ByteArrayOutputStream( ); + try (FileInputStream fis = new FileInputStream(path); + ByteArrayOutputStream bos = new ByteArrayOutputStream()) { int c; - while ( (c = fis.read()) != -1) - bos.write( c ); - pictureIndex = wb.addPicture( bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG ); - } - finally - { - if (fis != null) - fis.close(); - if (bos != null) - bos.close(); + while ((c = fis.read()) != -1) + bos.write(c); + pictureIndex = wb.addPicture(bos.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG); } return pictureIndex; } |