<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://triptibhatnagar.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Thu, 15 Oct 2026 05:12:54 GMT</lastBuildDate><atom:link href="https://triptibhatnagar.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[File Handling in JAVA]]></title><description><![CDATA[Hello Java Learners! 👋
“File Handling feels scary or confusing?”Not anymore! Let’s make it super simple and fun. ✨
Instead of diving into boring long theories (which you’ll find everywhere), here’s a blog that’ll help you grasp the real concept—beca...]]></description><link>https://triptibhatnagar.hashnode.dev/file-handling-in-java</link><guid isPermaLink="true">https://triptibhatnagar.hashnode.dev/file-handling-in-java</guid><category><![CDATA[Java]]></category><category><![CDATA[FileHandling]]></category><category><![CDATA[fileio]]></category><category><![CDATA[#javaio]]></category><category><![CDATA[beginnersguide]]></category><category><![CDATA[java development]]></category><category><![CDATA[General Programming]]></category><dc:creator><![CDATA[Tripti Bhatnagar]]></dc:creator><pubDate>Fri, 13 Jun 2025 21:22:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/fPkvU7RDmCo/upload/48338571bff7f42b81f968f7982b64e1.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-hello-java-learners"><strong>Hello Java Learners! 👋</strong></h3>
<p><strong>“File Handling feels scary or confusing?”</strong><br />Not anymore! Let’s make it <strong>super simple and fun</strong>. ✨</p>
<p>Instead of diving into boring long theories (which you’ll find everywhere), here’s a blog that’ll help you <strong>grasp the real concept</strong>—because once you get the idea, the code becomes a breeze!</p>
<hr />
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ol>
<li><p>What is File Handling?</p>
</li>
<li><p>Quick Look: File Operations</p>
</li>
<li><p>Java File Handling Classes &amp; Methods</p>
</li>
<li><p>Let’s Start Playing with Files!</p>
<ul>
<li><p>i. Creating a File</p>
</li>
<li><p>ii. Writing to a File</p>
</li>
<li><p>iii. Appending to a File</p>
</li>
<li><p>iv. Reading a File (3 Cool Ways!)</p>
</li>
<li><p>v. Deleting a File</p>
</li>
<li><p>Try-catch</p>
</li>
</ul>
</li>
<li><p>Summary Table</p>
</li>
</ol>
<hr />
<h2 id="heading-what-is-file-handling"># What is File Handling?</h2>
<p>File handling means telling Java:</p>
<p><em>“Hey, open this file, write something cool in it, or read it later. Also, throw it away if it’s useless.”</em></p>
<p>We use <strong>classes like</strong> File, FileWriter, Scanner, BufferedReader, etc., to do this.</p>
<hr />
<h2 id="heading-quick-look-file-operations"># Quick Look: File Operations</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Task</strong></td><td><strong>Class</strong></td><td><strong>Notes</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Create</td><td>File</td><td>.createNewFile()</td></tr>
<tr>
<td>Write</td><td>FileWriter</td><td>Overwrites content</td></tr>
<tr>
<td>Append</td><td>FileWriter</td><td>Use true flag</td></tr>
<tr>
<td>Read</td><td>Scanner</td><td>Line by line</td></tr>
<tr>
<td>Read</td><td>FileReader</td><td>Char by char</td></tr>
<tr>
<td>Read</td><td>BufferedReader</td><td>Super fast</td></tr>
<tr>
<td>Delete</td><td>File</td><td>.delete()</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-java-file-handling-classes-and-methods"># Java File Handling Classes and Methods</h2>
<ol>
<li><h3 id="heading-file-class"><em>File</em> Class</h3>
</li>
</ol>
<ul>
<li><p><strong>Purpose</strong>: Used to create, check, delete, or get details about files and directories.</p>
</li>
<li><p><strong>Key Methods</strong>:</p>
<ul>
<li><p><em>createNewFile()</em> : Creates a new file if it doesn’t exist.</p>
</li>
<li><p><em>delete()</em> : Deletes a file or directory.</p>
</li>
<li><p><em>exists()</em> : Checks if the file exists.</p>
</li>
<li><p><em>getName()</em> , <em>getAbsolutePath()</em>, etc.: To get file details.</p>
</li>
</ul>
</li>
</ul>
<ol start="2">
<li><h3 id="heading-filewriter-class"><em>FileWriter</em> Class</h3>
</li>
</ol>
<ul>
<li><p><strong>Purpose</strong>: Used to write character data to a file (overwrites by default).</p>
</li>
<li><p><strong>Key Constructors</strong>:</p>
<ul>
<li><p><em>FileWriter(String fileName)</em> : Creates writer to write (overwrites).</p>
</li>
<li><p><em>FileWriter(String filename, true)</em>: Appends to file if true is passed.</p>
</li>
</ul>
</li>
<li><p><strong>Key Methods</strong>:</p>
<ul>
<li><p><em>write(String)</em>: Writes the given text.</p>
</li>
<li><p><em>close()</em>: Closes the writer to save changes.</p>
</li>
</ul>
</li>
</ul>
<ol start="3">
<li><h3 id="heading-scanner-class"><em>Scanner</em> Class</h3>
</li>
</ol>
<ul>
<li><p><strong>Purpose</strong>: Reads files easily, especially line by line.</p>
</li>
<li><p><strong>Key Constructor</strong>:</p>
<ul>
<li><em>Scanner(File)</em> : Reads the file.</li>
</ul>
</li>
<li><p><strong>Key Methods</strong>:</p>
<ul>
<li><p><em>hasNextLine()</em>: Checks if there’s another line.</p>
</li>
<li><p><em>nextLine()</em>: Reads the next line.</p>
</li>
</ul>
</li>
</ul>
<ol start="4">
<li><h3 id="heading-filereader-class"><em>FileReader</em> Class</h3>
</li>
</ol>
<ul>
<li><p><strong>Purpose</strong>: Reads file character by character (low-level).</p>
</li>
<li><p><strong>Key Method</strong>:</p>
<ul>
<li><p><em>read()</em>: Reads one character at a time and returns ASCII code (int).</p>
</li>
<li><p><em>close()</em>: Closes the reader.</p>
</li>
</ul>
</li>
</ul>
<ol start="5">
<li><h3 id="heading-bufferedreader-class"><em>BufferedReader</em> Class</h3>
</li>
</ol>
<ul>
<li><p><strong>Purpose</strong>: Reads files line by line efficiently (faster than Scanner).</p>
</li>
<li><p><strong>Key Constructor</strong>:</p>
<ul>
<li><em>BufferedReader(new FileReader("filename"))</em>: Combines both.</li>
</ul>
</li>
<li><p><strong>Key Method</strong>:</p>
<ul>
<li><p><em>readLine()</em>: Reads an entire line.</p>
</li>
<li><p><em>close()</em>: Closes the reader.</p>
</li>
</ul>
</li>
</ul>
<hr />
<h2 id="heading-lets-start-playing-with-files"># Let’s Start Playing with Files!</h2>
<h3 id="heading-i-creating-a-file">i. Creating a File</h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.File;

File file = <span class="hljs-keyword">new</span> File(<span class="hljs-string">"data.txt"</span>); <span class="hljs-comment">// Creating File object for "data.txt"</span>
file.createNewFile(); <span class="hljs-comment">// Actually creates the file (if it doesn’t exist)</span>
</code></pre>
<p><em>📓Think of it like creating a</em> <strong><em>new notebook .</em></strong></p>
<hr />
<h3 id="heading-ii-writing-to-a-file">ii. Writing to a File</h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.FileWriter;

FileWriter fw = <span class="hljs-keyword">new</span> FileWriter(<span class="hljs-string">"data.txt"</span>);<span class="hljs-comment">// Create a FileWriter to write to the file</span>
fw.write(<span class="hljs-string">"Hello World!"</span>);<span class="hljs-comment">// Write a string to the file</span>
fw.close();<span class="hljs-comment">// Always close the file after writing</span>
</code></pre>
<p>📝 <em>Writes over old content like using a</em> <strong><em>pen on fresh page</em></strong>.</p>
<hr />
<h3 id="heading-iii-appending-to-a-file">iii. Appending to a File</h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.FileWriter;

FileWriter fw = <span class="hljs-keyword">new</span> FileWriter(<span class="hljs-string">"data.txt"</span>, <span class="hljs-keyword">true</span>);<span class="hljs-comment">// true enables append mode</span>
fw.write(<span class="hljs-string">"\nMore lines!"</span>);<span class="hljs-comment">//Adds new text without deleting existing data</span>
fw.close();<span class="hljs-comment">// Close the file to save changes</span>
</code></pre>
<p>📌 <em>Adds new stuff at the bottom like</em> <strong><em>sticking notes.</em></strong></p>
<hr />
<h3 id="heading-iv-reading-a-file-3-cool-ways">iv. Reading a File (3 Cool Ways!)</h3>
<h4 id="heading-a-using-scanner-line-by-line">a. Using Scanner (Line-by-Line)</h4>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.util.Scanner;
<span class="hljs-keyword">import</span> java.io.File;

Scanner sc = <span class="hljs-keyword">new</span> Scanner(<span class="hljs-keyword">new</span> File(<span class="hljs-string">"data.txt"</span>));<span class="hljs-comment">// Scanner to read the file</span>
<span class="hljs-keyword">while</span> (sc.hasNextLine()) <span class="hljs-comment">// Loop until no more lines</span>
    System.out.println(sc.nextLine());<span class="hljs-comment">// Print each line one by one</span>
sc.close();<span class="hljs-comment">// Close the scanner</span>
</code></pre>
<p>🔍 <em>Best for casual reading, like</em> <strong><em>reading a diary</em></strong>.</p>
<hr />
<h4 id="heading-b-using-filereader-char-by-char">b. Using FileReader (Char-by-Char)</h4>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.FileReader;

FileReader fr = <span class="hljs-keyword">new</span> FileReader(<span class="hljs-string">"data.txt"</span>);<span class="hljs-comment">// Create FileReader</span>
<span class="hljs-keyword">int</span> ch;<span class="hljs-comment">// To store characters read</span>
<span class="hljs-keyword">while</span>((ch = fr.read()) != -<span class="hljs-number">1</span>) <span class="hljs-comment">// Read one character at a time </span>
    System.out.print((<span class="hljs-keyword">char</span>) ch);<span class="hljs-comment">// Print the character</span>
fr.close(); <span class="hljs-comment">// Close the reader</span>
</code></pre>
<p>🔠 <em>Like reading alphabet soup letter by letter!</em></p>
<hr />
<h4 id="heading-c-using-bufferedreader-fast-amp-smooth">c. Using BufferedReader (Fast &amp; Smooth)</h4>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.BufferedReader;
<span class="hljs-keyword">import</span> java.io.FileReader;

BufferedReader br = <span class="hljs-keyword">new</span> BufferedReader(<span class="hljs-keyword">new</span> FileReader(<span class="hljs-string">"data.txt"</span>));<span class="hljs-comment">// Chain FileReader to BufferedReader</span>
String line;<span class="hljs-comment">// Stores each line</span>
<span class="hljs-keyword">while</span>((line = br.readLine()) != <span class="hljs-keyword">null</span>) <span class="hljs-comment">// Read until end</span>
    System.out.println(line);<span class="hljs-comment">// Print each line</span>
br.close();<span class="hljs-comment">// Close reader</span>
</code></pre>
<p>🚀 <em>Efficient and fast — perfect for big files!</em></p>
<hr />
<h3 id="heading-v-deleting-a-file">v. Deleting a File</h3>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.File;

File f = <span class="hljs-keyword">new</span> File(<span class="hljs-string">"data.txt"</span>);<span class="hljs-comment">// Reference to the file</span>
f.delete(); <span class="hljs-comment">// Deletes file (returns true if successful)</span>
</code></pre>
<p>🗑️ <em>Like throwing away an old paper.</em></p>
<hr />
<h2 id="heading-try-catch"># Try-catch</h2>
<p>Sometimes, things go wrong... maybe the file doesn't exist, or there's a typo in the filename. Java doesn’t like surprises, so it wants you to handle errors <strong>gracefully</strong> using a try-catch block!</p>
<p>Think of <em>try</em> as:<br />👉 “Let me try this risky task...”<br />And <em>catch</em> as:<br />👉 “If something breaks, I’ll catch the error and handle it nicely.”</p>
<pre><code class="lang-java"><span class="hljs-keyword">import</span> java.io.*;  <span class="hljs-comment">// Importing all necessary classes from java.io package</span>

<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Main</span> </span>{
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">// Creating a FileReader to read from "data.txt"</span>
            FileReader fr = <span class="hljs-keyword">new</span> FileReader(<span class="hljs-string">"data.txt"</span>);

            <span class="hljs-keyword">int</span> ch; <span class="hljs-comment">// Variable to hold each character (as int)</span>

            <span class="hljs-comment">// Read each character until end of file (-1)</span>
            <span class="hljs-keyword">while</span> ((ch = fr.read()) != -<span class="hljs-number">1</span>)
                System.out.print((<span class="hljs-keyword">char</span>) ch); <span class="hljs-comment">// Convert int to char and print it</span>

            fr.close(); <span class="hljs-comment">// Close the FileReader to free system resources</span>
        } <span class="hljs-keyword">catch</span> (IOException e) {
            <span class="hljs-comment">// If there's any error (e.g., file not found), print the error message</span>
            System.out.println(<span class="hljs-string">"Error: "</span> + e);
        }
    }
}
</code></pre>
<hr />
<h2 id="heading-summary-table">Summary Table</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Action</strong></td><td><strong>Code Signatures</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Create</td><td>new File().createNewFile()</td></tr>
<tr>
<td>Write</td><td>new FileWriter().write()</td></tr>
<tr>
<td>Append</td><td>new FileWriter(file, true).write()</td></tr>
<tr>
<td>Read (easy)</td><td>Scanner.nextLine()</td></tr>
<tr>
<td>Read (char)</td><td>FileReader.read()</td></tr>
<tr>
<td>Read (fast)</td><td>BufferedReader.readLine()</td></tr>
<tr>
<td>Delete</td><td>File.delete()</td></tr>
</tbody>
</table>
</div><hr />
<h3 id="heading-ready-to-conquer-file-handling">Ready to Conquer File Handling?</h3>
<p>Now you can teach someone else! Because when file handling becomes fun, you don’t skip it—you <strong>enjoy</strong> it! 😄</p>
]]></content:encoded></item></channel></rss>