<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nirmit Gupta</title>
    <description>The latest articles on DEV Community by Nirmit Gupta (@nirmit1910).</description>
    <link>https://dev.to/nirmit1910</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F731611%2Fbfdaa85c-340d-475e-b251-aa8b63fbacd2.jpg</url>
      <title>DEV Community: Nirmit Gupta</title>
      <link>https://dev.to/nirmit1910</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nirmit1910"/>
    <language>en</language>
    <item>
      <title>File Operations In JAVA</title>
      <dc:creator>Nirmit Gupta</dc:creator>
      <pubDate>Wed, 19 Feb 2025 21:41:25 +0000</pubDate>
      <link>https://dev.to/nirmit1910/file-operations-in-java-4n22</link>
      <guid>https://dev.to/nirmit1910/file-operations-in-java-4n22</guid>
      <description>&lt;p&gt;File handling is one of the most important aspects in world of coding.&lt;br&gt;
In JAVA we can perform the following by JAVA I/O API.&lt;/p&gt;

&lt;p&gt;File handling enables one to perform various operations related to create, read, delete, update a file.&lt;/p&gt;

&lt;p&gt;Before jumping into file handling lets understand the concept of Streams in JAVA.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Streams&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Streams are a sequence of character.&lt;br&gt;
There are two types of streams in JAVA.&lt;br&gt;
&lt;strong&gt;1.) Input Stream&lt;/strong&gt; — The input stream is used to read data from a file.&lt;br&gt;
It is an abstract class which provides common definition to be used by subclasses. We have various subclasses of InputStream such as —&lt;br&gt;
&lt;em&gt;.)AudioInputStream&lt;/em&gt; — It is used to read or manipulate audio data.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.)ByteArrayInputStream&lt;/em&gt; — Used to read byte array data as stream.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.)FileInputStream&lt;/em&gt; — This input stream subclass is used to read and manipulate data from files.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.)FilterInputStream&lt;/em&gt; — Provide additional functionality like buffering or data transformation.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.)StringBufferInputStream&lt;/em&gt; — Stream to read data from a string buffer.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.)ObjectInputStream&lt;/em&gt; — Used to read and deserialize objects from an input stream.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.)Output Stream&lt;/strong&gt; — The output stream is used to write data to various output devices such as monitor,file and many more.It is also an abstract class with various subclasses.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.)ByteArrayOutputStream&lt;br&gt;
.)FileOutputStream&lt;br&gt;
.)StringBufferOutputStream&lt;br&gt;
.)ObjectOutputStream&lt;br&gt;
.)DataOutputStream&lt;br&gt;
.)PrintStream&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Based on data we have two types of streams.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)Byte Stream&lt;/strong&gt;- This stream is used to read/write byte data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)Character Stream&lt;/strong&gt;- This stream is used to read/write character data.&lt;/p&gt;

&lt;p&gt;Since one has got idea on Streams and various stuff around it, lets comeback to File Handling in JAVA.&lt;/p&gt;

&lt;p&gt;Lets see a few methods related to file handling which will give idea of what all we can do in file handling.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)canRead()&lt;/strong&gt; — This method returns a boolean value which tells whether a file is readable or not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)createNewFile()&lt;/strong&gt; — This method returns a boolean value and creates a new file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)delete()&lt;/strong&gt; — This method returns a boolean value and deletes a file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)getName()&lt;/strong&gt; — This method returns a string value which is the name of the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)getAbsolutePath()&lt;/strong&gt; — This method returns a string value which is the absolute path of the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)length()&lt;/strong&gt; — This method returns a long value which is the size of the file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)list()&lt;/strong&gt; — This method returns a string array which contains names of all files present in the directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)mkdir()&lt;/strong&gt; — This method returns a boolean value and create a new directory.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)canWrite()&lt;/strong&gt; —This method returns a boolean value and checks whether we can write data into a file or not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;.)exists()&lt;/strong&gt; — This method return a boolean value and checks whether a file exists or not.&lt;/p&gt;

&lt;p&gt;With files one can do CRUD operations which are -&lt;br&gt;
&lt;strong&gt;.)Create a file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.File;
import java.io.IOException;

public class CreateFile {
    public static void main(String args[]) {
        try {
            // Create an object of a file
            File file = new File("Example.txt");
            //Check whether file exists or not
            if (file.createNewFile()) {
                System.out.println("File " + file.getName() + " got created successfully.");
            } else {
                System.out.println("File already exists in the directory.");
            }
        } catch (IOException exception) {
            System.out.println("Unexpected error is occurred.");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;File Example.txt got created successfully.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.)Read a file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ReadAFile {
    public static void main(String[] args) {
        File file = new File("Example.txt");
        try {
            Scanner sc = new Scanner(file);

            //Reading the file
            while (sc.hasNextLine()) {
                String data = sc.nextLine();
                System.out.println(data);
            }
            sc.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello World
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.)Write/Update a file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.FileWriter;

public class WriteAFile {
    public static void main(String[] args) {
        try {
            FileWriter file = new FileWriter("Example.txt");
            file.write("Hello World");

            //Close the stream
            file.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;.)Delete a file&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.File;

public class DeleteAFile {
    public static void main(String[] args) {
        File file = new File("Example.txt");
        try {
            if(file.delete()){
                System.out.println("File deleted successfully");
            }
            else{
                System.out.println("Exception occurred while deleting the file");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;File deleted successfully
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This marks the end of the tutorial.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In summary JAVA provides a robust mechanism to handle file operations in JAVA which makes development easy.My suggestion would be to explore more in this topic and play with more methods for better grasp of the topic.&lt;br&gt;
I hope the above tutorial provides some value to the reader and if you find this tutorial useful, do leave a comment and share it with fellow developers.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;!!Happy Learning.Happy Coding!!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>file</category>
      <category>coding</category>
      <category>programming</category>
    </item>
    <item>
      <title>Annotations in JAVA</title>
      <dc:creator>Nirmit Gupta</dc:creator>
      <pubDate>Tue, 18 Feb 2025 18:05:16 +0000</pubDate>
      <link>https://dev.to/nirmit1910/annotations-in-java-5836</link>
      <guid>https://dev.to/nirmit1910/annotations-in-java-5836</guid>
      <description>&lt;p&gt;JAVA Annotations are nothing but metadata for for programs.&lt;br&gt;
Annotations provide additional data for classes, interfaces, methods, fields which is used by JVM and compiler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax :-&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@AnnotationName
public class MyClass{...}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Predefined Java Annotations :-&lt;br&gt;
JAVA provides us with several built-in annotations.&lt;br&gt;
Lets explore them one by one.&lt;br&gt;
a) &lt;strong&gt;&lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
This annotations ensures that the method is been overriden from its superclass.&lt;br&gt;
In below example display method Child class is overriden from parent class. So it ensures that we inherit the correct method intended to be inherited. If we make some spelling mistake with method name and use override annotation, it will throw error.&lt;br&gt;
Error message will be as follows - "Method does not override method from its superclass"&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Parent {
    void display(){
        System.out.println("Hello from parent class");   
    }
}

class Child extends Parent{
    @Override
    void display(){
        System.out.println("Hello from child class");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;b) &lt;a class="mentioned-user" href="https://dev.to/deprecated"&gt;@deprecated&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
It provides information that following method or class is deprecated . Provides user that it may be removed in future versions.&lt;br&gt;
In below example it shows that method named "calcSum" will be deprecated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Sum {

    @Deprecated
    int calcSum(int a,int b){
        return a+b; 
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;c) @SuppressWarnings&lt;/strong&gt;&lt;br&gt;
Suppresses warnings issued by compilers. Helps to keep code clean by removing unnecessary warnings.&lt;br&gt;
Syntax-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@SuppressWarnings("warning_type")
class Sum {

    @Deprecated
    int calcSum(int a,int b){
        return a+b;
    }
}

public class Main {
    @SuppressWarnings("deprecation")
    public static void main(String[] args) {
        Sum sum=new Sum();
        int sumValue=sum.calcSum(1,2);
        System.out.println("Sum is="+sumValue);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In above example without annotation, we would get warning that calcSum is deprecated.&lt;/p&gt;

&lt;p&gt;Lets discuss about built-in annotations in JAVA which are used in custom annotations.&lt;br&gt;
&lt;strong&gt;a)@Target&lt;/strong&gt;&lt;br&gt;
Tells which type of element where annotation is to be applied like TYPE (class,enums,interface),FIELD,METHOD,CONSTRUCTOR,LOCAL_VARIABLE,ANNOTATION_TYPE,PARAMETER&lt;br&gt;
&lt;code&gt;@Target(ElementType.TYPE)&lt;/code&gt; &lt;br&gt;
&lt;strong&gt;b)@Retention&lt;/strong&gt;&lt;br&gt;
Specifies at which level annotation will be available.&lt;br&gt;
Ex- &lt;br&gt;
.)SOURCE - It tells that annotation is available at source code and ignored during compilation.&lt;br&gt;
.)CLASS - It tells that annotation is available to compiler and not JVM.&lt;br&gt;
.)RUNTIME - It tells that annotation is available to compiler and JVM.&lt;br&gt;
&lt;strong&gt;c)@Inherited&lt;/strong&gt;&lt;br&gt;
Tells that annotation is inherited by subclasses.&lt;br&gt;
&lt;code&gt;d)@Documented&lt;/code&gt;&lt;br&gt;
Ensueres annotation is documented in JavaDoc .&lt;/p&gt;

&lt;p&gt;Types of Annotations&lt;br&gt;
&lt;strong&gt;a)Marker Annotations&lt;/strong&gt;&lt;br&gt;
Annotations without methods are marker annotations&lt;br&gt;
Ex - &lt;a class="mentioned-user" href="https://dev.to/override"&gt;@override&lt;/a&gt; ,&lt;a class="mentioned-user" href="https://dev.to/deprecated"&gt;@deprecated&lt;/a&gt;&lt;br&gt;
&lt;code&gt;@interface MyAnnotations{}&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;b)Single-Value Annotations&lt;/strong&gt;&lt;br&gt;
Annotations with one method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@interface MyAnnotations{
  int num();
}

//Apply
@MyAnnotations(num=1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;c)Multi-Value Annotations&lt;/strong&gt;&lt;br&gt;
Annotations with more than one method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@interface MyAnnotations{
  int num1();
  int num2();
}

//Apply
@MyAnnotations(num1=1,num2=1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Custom Annotations&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lets try creating custom annotation which will provide much deeper understanding of the topic.&lt;br&gt;
a.)To define custom annotation we use the &lt;a class="mentioned-user" href="https://dev.to/interface"&gt;@interface&lt;/a&gt; keyword :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Retention(RetentionPolicy.RUNTIME)  // Available at runtime
@Target(ElementType.METHOD)          // Can be applied to methods only
@interface Review {
    String reviewerName() default "Unknown";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Over here we define annotation with name "Review" using &lt;a class="mentioned-user" href="https://dev.to/interface"&gt;@interface&lt;/a&gt; keyword .&lt;br&gt;
b.)Next step is to apply the annotation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class CodeReview {
    @Review(reviewerName = "John")
    public void ReviewMethod1() {
        System.out.println("I need review");
    }
    @Review(reviewerName = "Mike")
    public void ReviewMethod2() {
        System.out.println("I also need review");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both methods "ReviewMethod1" and "ReviewMethod2" have annotation "Review" which tells the name of the reviewer of those methods.&lt;br&gt;
c.) Now lets read and display these Annotations&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class MainExample {
    public static void main(String[] args) {
        Class&amp;lt;CodeReview&amp;gt; obj=CodeReview.class;
        for(Method method:obj.getDeclaredMethods()){
            if(method.isAnnotationPresent(Review.class)){
                Review annotation=method.getAnnotation(Review.class);
                System.out.println("Method:" +method.getName()+ "| Reviewer: "+annotation.reviewerName());
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;d.)Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Method:ReviewMethod1 | Reviewer: John
Method:ReviewMethod2 | Reviewer: Mike
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if you go through the code in "c" you will see have used JAVA Reflections API. It is a powerful feature to read and manipulate structure and behaviour of classes, methods and fields at runtime.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This marks the end of tutorial.&lt;br&gt;
We explored annotations, their syntax, types and how to write custom annotations.&lt;br&gt;
My suggestion would be to explore more in this topic and play with more annotations for better grasp of the topic.&lt;br&gt;
I hope the above tutorial provides some value to the reader and if you find this tutorial useful, do leave a comment and share it with fellow developers.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;!!Happy Learning.Happy Coding!!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>annotations</category>
      <category>programming</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
