{"id":1314,"date":"2022-02-27T18:04:05","date_gmt":"2022-02-27T18:04:05","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1314"},"modified":"2022-02-27T18:04:05","modified_gmt":"2022-02-27T18:04:05","slug":"file-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/27\/file-class\/","title":{"rendered":"\u00a0File Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about Java File and its various operations with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>File<\/code>&nbsp;class of the&nbsp;<code>java.io<\/code>&nbsp;package is used to perform various operations on files and directories.<\/p>\n\n\n\n<p>There is another package named&nbsp;<code>java.nio<\/code>&nbsp;that can be used to work with files. However, in this tutorial, we will focus on the&nbsp;<code>java.io<\/code>&nbsp;package.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"file-directory\">File and Directory<\/h2>\n\n\n\n<p>A file is a named location that can be used to store related information. For example,<\/p>\n\n\n\n<p><strong>main.java<\/strong>&nbsp;is a Java file that contains information about the Java program.<\/p>\n\n\n\n<p>A directory is a collection of files and subdirectories. A directory inside a directory is known as subdirectory.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-object\">Create a Java File Object<\/h2>\n\n\n\n<p>To create an object of&nbsp;<code>File<\/code>, we need to import the&nbsp;<code>java.io.File<\/code>&nbsp;package first. Once we import the package, here is how we can create objects of file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ creates an object of File using the path \nFile file = new File(String pathName);\n<\/code><\/pre>\n\n\n\n<p>Here, we have created a file object named&nbsp;<var>file<\/var>. The object can be used to work with files and directories.<\/p>\n\n\n\n<p><strong>Note<\/strong>: In Java, creating a file object does not mean creating a file. Instead, a file object is an abstract representation of the file or directory pathname (specified in the parenthesis).<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Java File Operation Methods<\/h2>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Operation<\/th><th>Method<\/th><th>Package<\/th><\/tr><tr><td>To create file<\/td><td><code>createNewFile()<\/code><\/td><td><code>java.io.File<\/code><\/td><\/tr><tr><td>To read file<\/td><td><code>read()<\/code><\/td><td><code>java.io.FileReader<\/code><\/td><\/tr><tr><td>To write file<\/td><td><code>write()<\/code><\/td><td><code>java.io.FileWriter<\/code><\/td><\/tr><tr><td>To delete file<\/td><td><code>delete()<\/code><\/td><td><code>java.io.File<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"create\">Java create files<\/h3>\n\n\n\n<p>To create a new file, we can use the&nbsp;<code>createNewFile()<\/code>&nbsp;method. It returns<\/p>\n\n\n\n<ul><li><code>true<\/code>&nbsp;if a new file is created.<\/li><li><code>false<\/code>&nbsp;if the file already exists in the specified location.<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Create a new File<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ importing the File class\nimport java.io.File;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create a file object for the current location\n    File file = new File(\"newFile.txt\");\n\n    try {\n\n      \/\/ trying to create a file based on the object\n      boolean value = file.createNewFile();\n      if (value) {\n        System.out.println(\"The new file is created.\");\n      }\n      else {\n        System.out.println(\"The file already exists.\");\n      }\n    }\n    catch(Exception e) {\n      e.getStackTrace();\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p>In the above example, we have created a file object named&nbsp;<var>file<\/var>. The file object is linked with the specified file path.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File file = new File(\"newFile.txt\");\n<\/code><\/pre>\n\n\n\n<p>Here, we have used the file object to create the new file with the specified path.<br><br><strong>If newFile.txt doesn&#8217;t exist in the current location<\/strong>, the file is created and this message is shown.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>The new file is created.\n<\/samp><\/pre>\n\n\n\n<p><strong>However, if newFile.txt already exists<\/strong>, we will see this message.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The file already exists.\n<\/samp><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"read\">Java read files<\/h3>\n\n\n\n<p>To read data from the file, we can use subclasses of either&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/inputstream\">InputStream<\/a>&nbsp;or&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/reader\">Reader<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Read a file using FileReader<\/h3>\n\n\n\n<p>Suppose we have a file named&nbsp;<strong>input.txt<\/strong>&nbsp;with the following content.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is a line of text inside the file. \n<\/code><\/pre>\n\n\n\n<p>Now let&#8217;s try to read the file using Java&nbsp;<code>FileReader<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ importing the FileReader class\nimport java.io.FileReader;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    char&#91;] array = new char&#91;100];\n    try {\n      \/\/ Creates a reader using the FileReader\n      FileReader input = new FileReader(\"input.txt\");\n\n      \/\/ Reads characters\n      input.read(array);\n      System.out.println(\"Data in the file:\");\n      System.out.println(array);\n\n      \/\/ Closes the reader\n      input.close();\n    }\n    catch(Exception e) {\n      e.getStackTrace();\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Data in the file:\nThis is a line of text inside the file.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used created an object of FileReader named input. It is now linked with the&nbsp;<strong>input.txt<\/strong>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileReader input = new FileReader(\"input.txt\");\n<\/code><\/pre>\n\n\n\n<p>To read the data from the&nbsp;<strong>input.txt<\/strong>&nbsp;file, we have used the read() method of&nbsp;<code>FileReader<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"write\">Java write to files<\/h3>\n\n\n\n<p>To write data to the file, we can use subclasses of either\u00a0OutputStream\u00a0or\u00a0Writer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Write to file using FileWriter<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ importing the FileWriter class\nimport java.io.FileWriter;\n\n class Main {\n   public static void main(String args&#91;]) {\n\n     String data = \"This is the data in the output file\";\n     try {\n       \/\/ Creates a Writer using FileWriter\n       FileWriter output = new FileWriter(\"output.txt\");\n\n       \/\/ Writes string to the file\n       output.write(data);\n       System.out.println(\"Data is written to the file.\");\n\n       \/\/ Closes the writer\n       output.close();\n     }\n     catch (Exception e) {\n       e.getStackTrace();\n     }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>Data is written to the file.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created a writer using the&nbsp;<code>FileWriter<\/code>&nbsp;class. The writer is linked with the&nbsp;<strong>output.txt<\/strong>&nbsp;file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FileWriter output = new FileWriter(\"output.txt\");\n<\/code><\/pre>\n\n\n\n<p>To write data to the file, we have used the&nbsp;<code>write()<\/code>&nbsp;method<code>.<\/code><\/p>\n\n\n\n<p>Here when we run the program, the&nbsp;<strong>output.txt<\/strong>&nbsp;file is filled with the following content.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>This is the data in the output file.\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"delete\">Java delete files<\/h3>\n\n\n\n<p>We can use the&nbsp;<code>delete()<\/code>&nbsp;method of the&nbsp;<var>File<\/var>&nbsp;class to delete the specified file or directory. It returns<\/p>\n\n\n\n<ul><li><code>true<\/code>&nbsp;if the file is deleted.<\/li><li><code>false<\/code>&nbsp;if the file does not exist.<\/li><\/ul>\n\n\n\n<p><strong>Note<\/strong>: We can only delete empty directories.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example: Delete a file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.File;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ creates a file object\n    File file = new File(\"file.txt\");\n\n    \/\/ deletes the file\n    boolean value = file.delete();\n    if(value) {\n      System.out.println(\"The File is deleted.\");\n    }\n    else {\n      System.out.println(\"The File is not deleted.\");\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The File is deleted.\n<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an object of File named file. The file now holds the information about the specified file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>File file = new File(\"file.txt\");\n<\/code><\/pre>\n\n\n\n<p>Here we have used the&nbsp;<code>delete()<\/code>&nbsp;method to delete the file specified by the object.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about Java File and its various operations with the help of examples. The&nbsp;File&nbsp;class of the&nbsp;java.io&nbsp;package is used to perform various operations on files and directories. There is another package named&nbsp;java.nio&nbsp;that can be used to work with files. However, in this tutorial, we will focus on the&nbsp;java.io&nbsp;package. File and Directory [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[597],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1314"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1314"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1314\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}