Thursday 30 June 2016

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


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
  • 1 comment: