Java Program to Append Text to an Existing File
What are File System?
In Java, a file system is a set of classes and interfaces that provide a standard way for reading and writing files and directories. The most commonly used file system classes are those in the java.io package, such as File, FileInputStream, and FileOutputStream.
In this Article, we will write a program to Count number of lines present in the file.
File System in Java :
In Java, the file system can be accessed and manipulated through the java.io and java.io packages.
- The java.io package provides classes such as File, FileInputStream, and FileOutputStream for working with files and directories. The File class can be used to create, delete, and manipulate files and directories. The FileInputStream and FileOutputStream classes can be used to read and write data to files, respectively.
- The java.io package, which was introduced in Java 1.4, provides advanced file system functionality such as file channel I/O and memory-mapped file I/O. The java.nio package includes classes such as Path, FileChannel, and MappedByteBuffer that can be used to perform low-level file system operations such as reading, writing, and manipulating file metadata.
- Additionally, the java.io.file package, which was introduced in Java 7, provides a more modern and versatile file system API. The java.nio.file package includes classes such as the Path, Files, and FileSystem classes, which can be used to perform various file system operations, including reading, writing, and manipulating file metadata.
Steps to Append Text to an Existing File :
- For the file you wish to append text to, create a File object.
- Using the File object produced in step 1, construct a FileWriter object.
- To append text to a file, use the FileWriter object’s write() function.
- FileWriter object should be closed.
Example 1 :
import java.io.*; public class Main { public static void main(String[] args) { // Create a File object for the file you want to append text to File file = new File("path/to/myfile.txt"); try { // Create a FileWriter object using the File object with append mode set to true FileWriter fw = new FileWriter(file, true); // Use the write() method of the FileWriter object to append the text to the file fw.write("This is some new text that we are appending to the file."); // Close the FileWriter object fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
Explanation:
The following code creates a File object for the file we want to append text to, uses that File object to create a FileWriter object with the append mode set to true, uses that File object’s write() method to append the text to the file, closes the FileWriter object, and appends the new text to the file.
Example 2 :
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class Main { public static void main(String[] args) { String filePath = "example.txt"; // specify the file path String textToAppend = "Text to append"; // specify the text to append try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath, true))) { writer.write(textToAppend); System.out.println("Text appended successfully."); } catch (IOException e) { System.err.println("An error occurred while appending text to the file."); e.printStackTrace(); } } }
Output :
Text appended successfully.
Explanation:
This program uses a BufferedWriter and a FileWriter to append text to an existing file. The FileWriter constructor takes two arguments: the file path and a boolean value indicating whether or not to append to the existing file. If this value is set to true, then data will be written to the end of the file rather than overwriting it.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment