Posts

Creating & Writing Text Files in Java (Part 6)

Image
  Pre-requisites:- Exception Handling , Reading Files   Creating a Text File A text file can be created by many ways , here I will try to show the most used ways to create a text file. Firstly , we will learn to create a text file by using  File class  which is by far the most popular way to create a text file.  First we have to create an instance of File class , then we have to call a method .createNewFile( ) which is a boolean i.e will return true or false. Also this method is suspicious of throwing an IOException  so we have to catch it using a try-catch block. If file is created the .createNewFile( ) returns true if file is created or if file name mentioned exists previously in any form returns a false value. import java.io.File; import java.io.IOException; public class MyClass { public static void main(String[] args) { File file = new File(" example.txt "); //this will create a text file named example try { if( file .createNew

Try-with resources in Java (Part 5)

Image
  Pre-requisites: Exception Handling , File Reading by Other ways Try-with Resources Try-with resources automatically closes the resources which are defined in it. We don't have to explicitly write the code to close those resources as it implements an interface called as AutoCloseable . This interface already has an abstract method called void close( ) which closes required resources.  Syntax:- try (resource defined) { //block of code } catch (exception 1) { //user exception prompt } Advantages of try-with resources  1. finally block not required to close the resource Let us compare a program by first using try-catch statements and then using same program with try-with resources. Using try-catch statement : import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class MyClass { public static void main(String[] args) {

Different ways to Read File (Part 4)

Image
  Pre-requisites: Read Files Using Scanner , Exception Handling Reading a File We learned to read a file using scanner class , now we will see other ways to read a text file in Java. 1. Using FileReader  FileReader is the most convenient class for reading files. The FileReader returns -1 after executing its code. -1 indicates that the contents have been read. import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class MyClass { public static void main(String[] args) { File file =new File(" C:\\Users\\Desktop\\example.txt "); try { FileReader fr =new FileReader( file ); int i =0; while(( i = fr .read())!= -1) { System.out.print((char) i ); //will read i in form of characters } fr .close(); } catch (FileNotFoundException e) { System.out.pri

Multiple Exceptions (Part 3)

Image
  Pre-requisites: Exception Handling Multiple Exceptions   In the last tutorial we learned to catch and handle exceptions , in this tutorial we will learn to catch and handle multiple exceptions at a time. As we learned that we can handle exceptions using throws statement or try-catch blocks . Now we will try to learn how to catch multiple exceptions from both  throws statement and try-catch blocks. 1. By using throws statement This feature was introduced in Java 7 that we can catch multiple exceptions at the same time. The syntax for the same is as follows: Syntax: public class MyClass { // using multiThrow statement public static void main(String[] args) throws Exception1, Exception2 , Exception3 { // block of code } } We can catch multiple exceptions by throws statement just by separating the exceptions by commas ( , ). The block of code will catch that exception which the main body of code would throw in the future.