{"id":424345,"date":"2026-03-08T18:04:38","date_gmt":"2026-03-08T17:04:38","guid":{"rendered":"https:\/\/dutable.com\/?p=424345"},"modified":"2026-03-08T19:08:12","modified_gmt":"2026-03-08T18:08:12","slug":"file-handling-in-java-guide","status":"publish","type":"post","link":"https:\/\/dutable.com\/file-handling-in-java-guide\/","title":{"rendered":"File Handling and Input\/Output (I\/O) in Java"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/dutable.com\/file-handling-in-java-guide\/#What_is_File_Handling_in_Java\" >What is File Handling in Java?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/dutable.com\/file-handling-in-java-guide\/#The_Essentials_of_File_Processing_Streams_Readers_and_Writers\" >The Essentials of File Processing: Streams, Readers, and Writers<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/dutable.com\/file-handling-in-java-guide\/#Types_of_Streams_in_Java\" >Types of Streams in Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/dutable.com\/file-handling-in-java-guide\/#Java_Reading_and_Writing_Files\" >Java Reading and Writing Files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/dutable.com\/file-handling-in-java-guide\/#Modifying_Files_in_Java\" >Modifying Files in Java<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/dutable.com\/file-handling-in-java-guide\/#When_to_Use_Byte_Streams_vs_Character_Streams\" >When to Use Byte Streams vs. Character Streams<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/dutable.com\/file-handling-in-java-guide\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">File handling and Input\/Output (I\/O) in Java plays a significant role in the interaction with the file system and the data persistence. Java has strong facilities in the process of creating, reading, writing, and altering the files with the help of streams, readers, and writers. To come up with strong Java applications, it is important to understand how these operations work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This paper will explore the different file processing algorithms in Java providing an example of the usage of streams, readers, and writers. It also compares between the character streams and the byte streams giving an insight of when to make use of each.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_File_Handling_in_Java\"><\/span><strong>What is File Handling in Java?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">File handling in Java means the capability of handling files, which includes the creation and reading, writing and editing of file contents. Java I\/O (Input\/Output) package offers an elaborate collection of classes which assist in the effective management of file operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Essentials_of_File_Processing_Streams_Readers_and_Writers\"><\/span><strong>The Essentials of File Processing: Streams, Readers, and Writers<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Streams are central to Java file handling. Streams are also abstractions that enable the flow of data in both directions i.e. towards a source and towards a destination and they enable the Java programs to deal with input and output operations. Java has two kinds of streams, namely, character streams and byte streams. They both have their applications and it is essential to know the differences so that you can implement the appropriate approach to your application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Types_of_Streams_in_Java\"><\/span><strong>Types of Streams in Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Byte Streams<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The raw binary data, an image file, audio file, or any other non-text data, is done by the use of the byte streams. These streams operate with 8-bit bytes and can be used to manipulate files who do not have data in the form of text.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Java implements the use of the following classes to have byte streams:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>FileInputStream<\/li>\n\n\n\n<li>FileOutputStream<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These classes both write and read data in binary format that is in the form of bytes hence making them the right classes to use when dealing with binary data. As an example, in case you are required to operate with media files or binary information, the most effective and precise option is the use of byte streams.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example of byte stream usage:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileInputStream;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileOutputStream;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.IOException;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class ExampleOfByteStream {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (FileInputStream fis = new FileInputStream(&#8220;input.txt&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileOutputStream fos = new FileOutputStream(&#8220;output.txt&#8221;)) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int content;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((content = fis.read()) != -1) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fos.write(content);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;File copied successfully with the use of the byte streams.&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Character Streams<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The character streams, in its turn, are a method of reading and writing data in the character format (16-bit Unicode characters). They are mainly applied to text files and they offer automatic character encoding. In contrast to the byte stream, character streams deal with the representation of data to and from a particular character encoding.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The character streams in Java are undertaken via:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>FileReader<\/li>\n\n\n\n<li>FileWriter<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Such classes are also effective in processing text files since they automatically deal with the issue of character encoding (e.g., UTF-8 or UTF-16).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Usage of character stream example:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileReader;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileWriter;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.IOException;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class CharacterStreamExample {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (FileReader fr = new FileReader(&#8220;input.txt&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FileWriter fw = new FileWriter(&#8220;output.txt&#8221;)) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int content;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((content = fr.read()) != -1) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fw.write(content);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;File copied successfully using character streams.&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Java_Reading_and_Writing_Files\"><\/span><strong>Java Reading and Writing Files<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Writing to a File<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java offers classes such as FileWriter, BufferedWriter, and PrintWriter in order to write data to a file. Such classes enable us to write characters effectively to files.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using FileWriter<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The easiest method of writing characters to a file is through FileWriter. It writes directly characters and in case the file is not found, it will create it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example of using FileWriter:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileWriter;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.IOException;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class FileWriterExample {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (FileWriter writer = new FileWriter(&#8220;example.txt&#8221;)) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.write(&#8220;Hello, World!&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Successful data writing.&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using BufferedWriter<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">FileWriter has an improved version BufferedWriter. It delays data with the help of a buffer and writes it in big batches, minimizing the number of write operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A sample exercise on BufferedWriter is as follows:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.BufferedWriter;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileWriter;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.IOException;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class BufferedWriterExample {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (BufferedWriter writer = new BufferedWriter(new FileWriter(&#8220;buffered_example.txt&#8221;))) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.write(&#8220;Buffered Writing is more productive.&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;Data written using BufferedWriter.&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Reading from a File<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Java offers FileReader, BufferedReader, and Scanner to efficiently read data in a file.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using FileReader<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">FileReader reads the characters of every letter in the file. Though it is a simple one, not quite efficient on bigger files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example of using FileReader:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileReader;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.IOException;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class FileReaderExample {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (FileReader reader = new FileReader(&#8220;example.txt&#8221;)) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int content;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((content = reader.read()) != -1) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.print((char) content);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Using BufferedReader<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">BufferedReader is applied in order to read text in an efficient way in a text file through buffering of the input. It is more so when dealing with large files or when the data has to be read line by line.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following is the example of the use of BufferedReader:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.BufferedReader;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileReader;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.IOException;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">class BufferedReaderExample {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (BufferedReader reader = new BufferedReader(new FileReader(&#8220;example.txt&#8221;))) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String line;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((line = reader.readLine()) != null) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(line);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Modifying_Files_in_Java\"><\/span><strong>Modifying Files in Java<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Java can be used to read and write files with a mixture of reading and writing functions. The file can be read and its contents manipulated and the data that is manipulated written back to the file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example of Modifying a File:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.BufferedReader;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.BufferedWriter;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileReader;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.FileWriter;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import java.io.IOException;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">public class ModifyFileExample {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;public static void main(String[] args) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try (BufferedReader reader = new BufferedReader(new FileReader(&#8220;example.txt&#8221;));<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BufferedWriter writer = new BufferedWriter(new FileWriter(&#8220;example.txt&#8221;, true))) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String line;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while ((line = reader.readLine()) != null) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.write(line);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.newLine();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.write(&#8220;New content added.&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&#8220;File modified successfully.&#8221;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} catch (IOException e) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"When_to_Use_Byte_Streams_vs_Character_Streams\"><\/span><strong>When to Use Byte Streams vs. Character Streams<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Byte Streams<\/strong>: Use when dealing with binary data (e.g., images, audio, video files).<\/li>\n\n\n\n<li><strong>Character Streams<\/strong>: Use when working with text data (e.g., .txt, .html, .xml files). They handle character encoding automatically, making them ideal for text-based files.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span><strong>Conclusion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we have explored how Java manages file operations using streams, readers, and writers. Understanding the differences between byte streams and character streams is essential for selecting the appropriate I\/O mechanism for your application. By using the provided examples, you can efficiently create, read, write, and modify files programmatically in Java.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>File handling and Input\/Output (I\/O) in Java plays a significant role in the interaction with the file system and the data persistence. Java has strong facilities in the process of creating, reading, writing, and altering the files with the help of streams, readers, and writers. To come up with strong Java applications, it is important&#8230;<\/p>\n","protected":false},"author":1905,"featured_media":424347,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30700,30705],"tags":[39257,39215,3917],"class_list":["post-424345","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","category-software-development","tag-file-handling","tag-java","tag-programming"],"_links":{"self":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/424345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/users\/1905"}],"replies":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/comments?post=424345"}],"version-history":[{"count":2,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/424345\/revisions"}],"predecessor-version":[{"id":424358,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/posts\/424345\/revisions\/424358"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media\/424347"}],"wp:attachment":[{"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/media?parent=424345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/categories?post=424345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dutable.com\/wp-json\/wp\/v2\/tags?post=424345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}