Found 7442 Articles for Java

Check if a file exists in Java

karthikeya Boyini
Updated on 18-Jun-2020 13:22:20

5K+ Views

The java.io.File class provides useful methods on file. This example shows how to check a file existence by using the file.exists() method of File class.Exampleimport java.io.File; public class Main {    public static void main(String[] args) {       File file = new File("C:/java.txt");       System.out.println(file.exists());    } }ResultThe above code sample will produce the following result (if the file "java.txt" exists in 'C' drive).trueExampleThe following is another simple example of the file exist or not in java.import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintpWriter; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public ... Read More

Check if a file exists in Java

karthikeya Boyini
Updated on 18-Jun-2020 13:22:20

5K+ Views

The java.io.File class provides useful methods on file. This example shows how to check a file existence by using the file.exists() method of File class.Exampleimport java.io.File; public class Main {    public static void main(String[] args) {       File file = new File("C:/java.txt");       System.out.println(file.exists());    } }ResultThe above code sample will produce the following result (if the file "java.txt" exists in 'C' drive).trueExampleThe following is another simple example of the file exist or not in java.import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintpWriter; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public ... Read More

Character Stream vs Byte Stream in Java

Samual Sam
Updated on 18-Jun-2020 13:25:51

10K+ Views

Byte StreamsJava byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream. Following is an example which makes use of these two classes to copy an input file into an output file −Exampleimport java.io.*; public class CopyFile {    public static void main(String args[]) throws IOException {               FileInputStream in = null;       FileOutputStream out = null;       try {          in = new ... Read More

Character Stream vs Byte Stream in Java

Samual Sam
Updated on 18-Jun-2020 13:25:51

10K+ Views

Byte StreamsJava byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are, FileInputStream and FileOutputStream. Following is an example which makes use of these two classes to copy an input file into an output file −Exampleimport java.io.*; public class CopyFile {    public static void main(String args[]) throws IOException {               FileInputStream in = null;       FileOutputStream out = null;       try {          in = new ... Read More

Chained exception in Java

karthikeya Boyini
Updated on 18-Jun-2020 13:30:06

2K+ Views

Chained exception helps to relate one exception to other. Often we need to throw a custom exception and want to keep the details of an original exception that in such scenarios we can use the chained exception mechanism. Consider the following example, where we are throwing a custom exception while keeping the message of the original exception.ExampleLive Demopublic class Tester {    public static void main(String[] args) {       try {          test();       }catch(ApplicationException e) {                    System.out.println(e.getMessage());       }    } ... Read More

Chained exception in Java

karthikeya Boyini
Updated on 18-Jun-2020 13:30:06

2K+ Views

Chained exception helps to relate one exception to other. Often we need to throw a custom exception and want to keep the details of an original exception that in such scenarios we can use the chained exception mechanism. Consider the following example, where we are throwing a custom exception while keeping the message of the original exception.ExampleLive Demopublic class Tester {    public static void main(String[] args) {       try {          test();       }catch(ApplicationException e) {                    System.out.println(e.getMessage());       }    } ... Read More

Can we override private methods in Java

Samual Sam
Updated on 18-Jun-2020 13:35:44

1K+ Views

Ideally No. But, using the tricky code, a subclass can override a private method as well. See the example below −ExampleLive Democlass A {    private void display() {       System.out.println("A.display");    }     public void callDisplay() {       System.out.println("A.callDisplay");       display();    } } class B extends A {    private void display() {       System.out.println("B.display");    }     public void callDisplay() {       System.out.println("B.callDisplay");       display();    }   } public class Tester {    public static void main(String[] args) {   ... Read More

Can we override private methods in Java

Samual Sam
Updated on 18-Jun-2020 13:35:44

1K+ Views

Ideally No. But, using the tricky code, a subclass can override a private method as well. See the example below −ExampleLive Democlass A {    private void display() {       System.out.println("A.display");    }     public void callDisplay() {       System.out.println("A.callDisplay");       display();    } } class B extends A {    private void display() {       System.out.println("B.display");    }     public void callDisplay() {       System.out.println("B.callDisplay");       display();    }   } public class Tester {    public static void main(String[] args) {   ... Read More

Calling a method using null in Java

karthikeya Boyini
Updated on 18-Jun-2020 12:27:06

695 Views

When a method is invoked on a null reference, it throws NullPointerException but in case of the static method, we can make it possible using cast expression. See the example below −ExampleLive Demopublic class Tester {    public static void display(){       System.out.println("display");    }    private void print() {       System.out.println("print");    }    public static void main(String[] args) {       //Scenario 1:       //Calling a method on null reference       //causes NullPointerException       try {          Tester test = null;     ... Read More

Calling a method using null in Java

karthikeya Boyini
Updated on 18-Jun-2020 12:27:06

695 Views

When a method is invoked on a null reference, it throws NullPointerException but in case of the static method, we can make it possible using cast expression. See the example below −ExampleLive Demopublic class Tester {    public static void display(){       System.out.println("display");    }    private void print() {       System.out.println("print");    }    public static void main(String[] args) {       //Scenario 1:       //Calling a method on null reference       //causes NullPointerException       try {          Tester test = null;     ... Read More

Advertisements