Found 4338 Articles for Java 8

What is an I/O filter in Java?

Debarpito Sarkar
Updated on 05-Sep-2022 12:33:42

704 Views

This article will help you understand what I/O filter in Java is. The Java I/O Filter The Java I/O Filter is inside java.io package. It provides sets of input and output streams used for reading and writing data to input and output sources. There are different types of classes in java.io, naming Input Stream, Output Stream, etc. Some of the important types are discussed below − Input Stream The InputStream class of java.io is an abstract superclass which reads data from an input source. The source can be a file, a string, or anything that can contain data. This class ... Read More

What are I/O classes in Java?

Swarali Sree
Updated on 25-Feb-2020 10:31:37

3K+ Views

The java.io package contains nearly every class you might ever need to perform input and output (I/O) in Java. All these streams represent an input source and an output destination. The stream in the java.io package supports many data such as primitives, object, localized characters, etc.A stream can be defined as a sequence of data. There are two kinds of Streams −InputStream: The InputStream is used to read data from a source.OutputStream: The OutputStream is used for writing data to a destination.As described earlier, a stream can be defined as a sequence of data. The InputStream is used to read data from ... Read More

How to check if a file exists or not in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:20

309 Views

The File class provides exists() method this returns true if a file in the specified path exists else it returns false.ExampleLive Demoimport java.io.File; public class FileHandling {    public static void main(String args[]) {       File file = new File("samplefile");       if(file.exists()) {          System.out.println("Given file existed");       } else {          System.out.println("Given file does not existed");       }    } }OutputGiven file does not existed

How to capture file not found exception in Java?

karthikeya Boyini
Updated on 20-Feb-2020 06:58:29

291 Views

While using FileInputStream, FileOutputStream, and RandomAccessFile classes, we need to pass the path of the file to their constructors. In case of a file in the specified path does not exist a FileNotFoundException is raised.Examplepublic class Sample {    public static void main(String args[]) throws Exception {       File file = new File("myFile");       FileInputStream fis = new FileInputStream(file);       System.out.println("Hello");    } }OutputException in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified)       at java.io.FileInputStream.open(Native Method)       at java.io.FileInputStream.open(Unknown Source)       at java.io.FileInputStream.(Unknown Source) ... Read More

How to capture out of array index out of bounds exception in Java?

Swarali Sree
Updated on 20-Feb-2020 06:51:48

348 Views

When you try to access an element of an array at an index which is out of range, an ArrayIndexOutOfBoundsException exception is raised.ExampleLive Demopublic class ArrayIndexOutOfBounds {    public static void main(String args[]) {       try {          int[] a = new int[]{1,2,3,4,5};          int x = 6;          a[10] = x;       } catch(ArrayIndexOutOfBoundsException ex) {          System.out.println("Array size is restricted to 5 elements only");       }    } }OutputArray size is restricted to 5 elements only

How to capture divide by zero exception in Java?

Swarali Sree
Updated on 20-Feb-2020 06:50:45

1K+ Views

When you divide a number by zero an Arithmetic Exception number is thrown.ExampleLive Demopublic class DividedByZero {    public static void main(String args[]) {       int a, b;       try {          a = 0;          b = 54/a;          System.out.println("hello");       } catch (ArithmeticException e) {          System.out.println("you cannot divide a number with zero");       }    } }Outputyou cannot divide a number with zero

Does finally always execute in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

306 Views

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.

What is the difference between throw and throws keywords in Java?

Sharon Christine
Updated on 20-Feb-2020 06:39:20

295 Views

The throw keyword is used to raise an exception explicitly.Examplepublic class Test {    public static void main(String[] args) {       throw new NullPointerException();    } }Exception in thread "main" java.lang.NullPointerException at a6.dateAndTime.Test.main(Test.java:5)The throws keywords in Java used to postpone the handling of a checked exception.public class Test {    public static void main(String[] args)throws NullPointerException {       throw new NullPointerException();    } }

What is the catch block in Java?

Swarali Sree
Updated on 25-Feb-2020 10:42:48

275 Views

A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in the try block, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.Exampleimport java.io.File; import java.io.FileInputStream; public class Test {    public static void main(String args[]) {       System.out.println("Hello");       try {          File file = new File("my_file");       ... Read More

What is the try block in Java?

Samual Sam
Updated on 25-Feb-2020 10:44:22

411 Views

A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following –Syntaxtry {    // Protected code } catch (ExceptionName e1) {    // Catch block }The code which is prone to exceptions is placed in the try block. When an exception raised inside a try block, instead of terminating the program JVM stores the exception details in the exception stack and proceeds to the catch block.

Advertisements