java在txt上追加文本
时间: 2024-04-12 13:30:16 浏览: 14
您可以使用Java中的FileWriter类来追加文本到一个文本文件中。下面是一个示例代码:
```java
import java.io.FileWriter;
import java.io.IOException;
public class AppendTextToFile {
public static void main(String[] args) {
String filePath = "path/to/your/file.txt";
String textToAppend = "This is the text to be appended.";
try {
FileWriter writer = new FileWriter(filePath, true); // 第二个参数设置为true表示追加文本
writer.write(textToAppend);
writer.close();
System.out.println("Text appended successfully.");
} catch (IOException e) {
System.out.println("An error occurred while appending the text.");
e.printStackTrace();
}
}
}
```
在上面的示例中,您需要将`filePath`变量的值更改为要追加文本的文件的实际路径。然后,将`textToAppend`变量的值更改为您要追加到文件中的实际文本。运行该程序后,指定的文本将被追加到指定的文件中。