Thursday 30 June 2016

Java Tutorial : Java Exception handling (Try with resources)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=95Cr46hO52M&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : Java Exception handling (Try with resources) 
Java Tutorial : Java Exception handling (Try with resources) 
Java Tutorial : Java Exception handling (Try with resources) 
Java Tutorial : Java Exception handling (Try with resources) 
ExceptionHandlingDemo.java
import java.io.FileInputStream;
import java.io.IOException;

public class ExceptionHandlingDemo
{

    public static void main(String[] args)
    {
        FileInputStream fileInputStream = null;
        try
        {
            fileInputStream = new FileInputStream("myfile.txt");
            int data = fileInputStream.read();
            while (data != -1)
            {
                System.out.print((char) data);
                data = fileInputStream.read();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (fileInputStream != null)
            {
                try
                {
                    fileInputStream.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

    }
}
Output
 Hi

TryWithResourcesDemo.java
import java.io.FileInputStream;
import java.io.IOException;

public class TryWithResourcesDemo
{

    public static void main(String[] args) throws IOException
    {
        try (FileInputStream fileInputStream = new FileInputStream("myfile.txt"))
        {
            int data = fileInputStream.read();
            while (data != -1)
            {
                System.out.print((char) data);
                data = fileInputStream.read();
            }
        }

    }

}
Output
Hi

Refer:
https://docs.oracle.com/javase/8/docs/api/index.html?java/lang/AutoCloseable.html

Click the below link to download the code:
https://sites.google.com/site/javaee4321/java/ExceptionDemo_try_with_resources_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4babce59a384cb4ab41f12b03cd7d11227a4b186/BasicJava/ExceptionDemo_try_with_resources_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
  • No comments:

    Post a Comment