The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java Tutorial : Java Exception handling (The Try with resources statement-two resources)

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ram N

Posts: 2777
Nickname: ramram
Registered: Jul, 2014

Ram N is Java Programmer
Java Tutorial : Java Exception handling (The Try with resources statement-two resources) Posted: Jun 30, 2016 9:15 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ram N.
Original Post: Java Tutorial : Java Exception handling (The Try with resources statement-two resources)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies. 1.J2EE 2.Java 3.Spring 4.Hibernate 5.Database 6.Oracle 7.Mysql 8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE

Advertisement

Click here to watch in Youtube :
https://www.youtube.com/watch?v=EXyRvT-jc9U&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Exception handling (The Try with resources statement-two resources) 
TryWithResourcesDemo.java
public class TryWithResourcesDemo
{

public static void main(String[] args) throws Exception
{
TryWithResourcesDemo tryWithResourcesDemo = new TryWithResourcesDemo();
tryWithResourcesDemo.writeToFileZipFileContents("files.zip",
"myfile.txt");
System.out
.println("Open zip file and create output file is done.");

}

/*
* Retrieves the names of the files packaged in the zip
* file zipFileName and creates a text file that
* contains the names of these files.
*/


public void writeToFileZipFileContents(String zipFileName,
String outputFileName) throws java.io.IOException
{

java.nio.charset.Charset charset = java.nio.charset.StandardCharsets.US_ASCII;
java.nio.file.Path outputFilePath = java.nio.file.Paths
.get(outputFileName);

// Open zip file and create output file with
// try-with-resources statement

try (java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(
zipFileName);
java.io.BufferedWriter bufferedWriter = java.nio.file.Files
.newBufferedWriter(outputFilePath, charset))
{

// Enumerate each entry
for (java.util.Enumeration entries = zipFile.entries(); entries
.hasMoreElements();)
{
// Get the entry name and write it to the
// output file
String newLine = System.getProperty("line.separator");
String zipEntryName = ((java.util.zip.ZipEntry) entries
.nextElement()).getName() + newLine;
bufferedWriter.write(zipEntryName, 0,
zipEntryName.length());
}
}
}
}
Output
Open zip file and create output file is done.
Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ExceptionDemo_the_try_with_resources_two_zip_writter_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ExceptionDemo_the_try_with_resources_two_zip_writter_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4babce59a384cb4ab41f12b03cd7d11227a4b186/BasicJava/ExceptionDemo_the_try_with_resources_two_zip_writter_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Read: Java Tutorial : Java Exception handling (The Try with resources statement-two resources)

    Topic: Servants, not leaders, not managers Previous Topic   Next Topic Topic: Insert Items to DynamoDB Tables using Java

    Sponsored Links



    Google
      Web Artima.com   

    Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use