📜  在现有文件中追加字符串的Java程序

📅  最后修改于: 2022-05-13 01:54:19.518000             🧑  作者: Mango

在现有文件中追加字符串的Java程序

在Java中,我们可以使用 FileWriter 在现有文件中附加一个字符串,该文件具有以附加模式打开文件的选项。 Java FileWriter 类用于将面向字符的数据写入文件。它是一个面向字符的类,用于Java中的文件处理。与 FileOutputStream 类不同,我们不需要将字符串转换为字节数组,因为它提供了直接写入字符串的方法。

让我们看看使用的构造函数 后来坚持使用此类的常规方法

构造函数: FileWriter(文件文件,布尔附加):

它构造一个 FileWriter 对象,给定一个附加模式的 File 对象。现在让我们切换到此处调用的此类的方法,并在将字符串附加到现有文件中发挥关键作用,如下所示:

方法一: write()

此方法写入 String 的一部分

句法:

void write(String s,int off,int len);

返回类型:无效 

参数:

  • 输入字符串
  • 关闭
  • 字符串长度

方法2:关闭()

此方法在刷新后关闭流。

返回类型:无效

例子

Java
// Java Program to Append a String to the
// End of a File
 
// Importing input output classes
import java.io.*;
 
// Main class
class GeeksforGeeks {
 
    // Method 1
    // TO append string into a file
    public static void appendStrToFile(String fileName,
                                       String str)
    {
        // Try block to check for exceptions
        try {
 
            // Open given file in append mode by creating an
            // object of BufferedWriter class
            BufferedWriter out = new BufferedWriter(
                new FileWriter(fileName, true));
 
            // Writing on output stream
            out.write(str);
            // Closing the connection
            out.close();
        }
 
        // Catch block to handle the exceptions
        catch (IOException e) {
 
            // Display message when exception occurs
            System.out.println("exception occurred" + e);
        }
    }
 
    // Method 2
    // main driver method
    public static void main(String[] args) throws Exception
    {
        // Creating a sample file with some random text
        String fileName = "Geek.txt";
 
        // Try block to check for exceptions
        try {
 
            // Again operating same operations by passing
            // file as
            // parameter to read it
            BufferedWriter out = new BufferedWriter(
                new FileWriter(fileName));
 
            // Writing on. file
            out.write("Hello World:\n");
 
            // Closing file connections
            out.close();
        }
 
        // Catch block to handle exceptions
        catch (IOException e) {
 
            // Display message when error occurs
            System.out.println("Exception Occurred" + e);
        }
 
        // Now appendinggiven str to above
        // created file
        String str = "This is GeeksforGeeks";
 
        // Calling the above method
        appendStrToFile(fileName, str);
 
        // Let us print modified file
        try {
            BufferedReader in = new BufferedReader(
                new FileReader("Geek.txt"));
 
            String mystring;
 
            // TIll there is content in string
            // condition holds true
            while ((mystring = in.readLine()) != null) {
                System.out.println(mystring);
            }
        }
 
        // Catch block to handle IO exceptions
        catch (IOException e) {
            System.out.println("Exception Occurred" + e);
        }
    }
}


输出: