在 Java 中将文本附加到另外文本文件
本文介绍了如何在 Java 中向一个现有的文本文件追加文本。我们还列出了一些示例代码以更好地指导你。
读取和写入是你在处理文件时可以执行的两项最基本的操作。在 Java 中有多种方法可以将数据写入或附加到文件中。在本文中,我们将了解附加到文本文件的四种不同方式。
使用 Java 中的 FileOutputStream
类将文本附加到文本文件
FileOutputStream
类用于将二进制数据写入文件。它主要用于 int
或 float
等原始数据类型,但它也可用于编写面向字符的数据。
字符串在写入之前首先需要转换为字节数组;为此,你可以使用 getBytes()
方法。我们还需要将 true
作为第二个参数传递给它的构造函数。第二个参数用于布尔追加标志,它的完成表明我们正在尝试追加到现有文件。
如果它被忽略,那么 FileOutputStream
类将覆盖文件的内容。如果文件不在上述位置,它将创建一个新文件。
这是一个示例,我们尝试将句子 The quick brown fox jumps over the lazy dog
附加到现有文件中。\r
和 \n
用于在新行的开头插入文本。
import java.io.FileOutputStream;
public class Main
{
public static void main(String args[])
{
try
{
String filePath = "C:\\Users\\Lenovo\\Desktop\\demo.txt";
FileOutputStream f = new FileOutputStream(filePath, true);
String lineToAppend = "\r\nThe quick brown fox jumps over the lazy dog";
byte[] byteArr = lineToAppend.getBytes(); //converting string into byte array
f.write(byteArr);
f.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
使用 Java 中的 FileWriter
类将文本附加到文本文件
Java FileWriter
类旨在将字符流写入文件。我们可以使用它来将字符或字符串附加到现有文件或新文件中。如果该文件不存在于上述路径中,则会创建一个新文件,并将数据写入此新文件。
与之前的 FileOutputStream
类不同,我们不需要在写入之前将字符串转换为字节。但是,我们还需要将 true
作为第二个参数传递给 FileWriter
构造函数,这表明我们正在尝试附加到现有文件。
如果省略,它将覆盖文件的现有内容。下面的代码演示了如何使用 FileWriter
向现有文件追加一行。
import java.io.FileWriter;
public class SimpleTesting
{
public static void main(String args[])
{
try
{
String filePath = "C:\\Users\\Lenovo\\Desktop\\demo.txt";
FileWriter fw = new FileWriter(filePath, true);
String lineToAppend = "\r\nThe quick brown fox jumps over the lazy dog";
fw.write(lineToAppend);
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
使用 Java 中的 BufferedWriter
类将文本附加到文本文件
FileWriter
类通常由其他编写器包装,例如 BufferedWriter
。完成此过程是因为 BufferedWriter
比 FileWriter
高效得多。
当我们有多个写操作时,BufferedWriter
比 FileWriter
更受欢迎。它使用内部缓冲存储器来存储必须附加到文件中的数据。当此缓冲区已满,或者我们不再有任何其他内容要附加时,BufferedWriter
将写入实际文件。
它将大块数据从其缓冲区写入文件,而不是频繁写入;他导致较少的系统调用。因此,它比普通的 FileWriter
高效得多。
但是如果我们只有一个写操作,那么 BufferedWriter
会在写过程中添加另一个步骤。所以,最好只使用 FileWriter
。
要使用 BufferedWriter
,我们首先需要一个 FileWriter
对象,我们将把它传递给 BufferedWriter
的构造函数。确保对附加标志使用 true
。
以下代码演示了 BufferedWriter
在 Java 中的工作方式。
import java.io.FileWriter;
import java.io.BufferedWriter;
public class Main
{
public static void main(String args[])
{
try
{
String filePath = "C:\\Users\\Lenovo\\Desktop\\demo.txt";
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
String lineToAppend = "\r\nThe quick brown fox jumps over the lazy dog";
bw.write(lineToAppend);
bw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
使用 Java 中的 Files
类将文本附加到文本文件
Files
实用程序类是在 Java 7 中添加的,它提供了另一种将数据写入文件的方法;我们使用 Files.write()
方法来做到这一点。此方法需要文件路径,还需要附加字符串的字节数组。
我们还需要指定 StandardOpenOption.APPEND
将文本附加到现有文件。确保文件存在于给定的路径中;否则,它将返回一个 NoSuchFileException
。
如果我们必须多次写入同一个文件,这种方法不是首选,因为该文件也会被多次打开和关闭,从而降低整体性能。
下面的代码演示了 Files.write()
方法如何在 Java I/O 中工作。
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Main
{
public static void main(String args[])
{
try
{
String file = "C:\\Users\\Lenovo\\Desktop\\demo.txt";
String lineToAppend = "\r\nThe quick brown fox jumps over the lazy dog";
byte[] byteArr = lineToAppend.getBytes();
Files.write(Paths.get(file), byteArr, StandardOpenOption.APPEND);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
在 Java 中将文本附加到文本文件
Java 提供了多种文件处理选项。我们可以使用 FileOutputStream
、FileWriter
、BufferedWriter
或 Files
实用程序类将数据附加到文件中。
对于 FileOutputStream
和 Files
实用程序类,我们首先需要将字符串转换为字节数组,然后将其传递给 write()
方法。
当我们必须多次将少量数据写入同一个文件时,首选 BufferedWriter
。另一方面,FileWriter
和 Files
实用程序类将为大量写入进行多个系统调用;这会降低它们的整体性能。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
在 Pandas 中读取 Excel 多张工作表
发布时间:2024/04/24 浏览次数:1450 分类:Python
-
本教程演示如何在 Pandas Python 中从 Excel 工作簿中读取多个 Excel 工作表。