java追加到文件末尾

Today we will look into how to append to a file in java. Java append to file is a common java IO operation. For example, whenever we print something to server logs, it gets appended to the existing file.

今天,我们将研究如何在Java中附加文件。 Java追加到文件是一种常见的Java IO操作。 例如,每当我们将某些内容打印到服务器日志时,它就会被附加到现有文件中。

Java追加到文件 (Java append to file)

We can append to file in java using following classes.

我们可以使用以下类在Java中追加文件。

  1. Java append to file using FileWriterJava使用FileWriter附加到文件
  2. Java append content to existing file using BufferedWriterJava使用BufferedWriter将内容追加到现有文件
  3. Append text to file in java using PrintWriter使用PrintWriter将文本追加到Java中的文件
  4. Append to file in java using FileOutputStream使用FileOutputStream附加到Java中的文件

If you are working on text data and the number of write operations is less, use FileWriter and use its constructor with append flag value as true. If the number of write operations is huge, you should use the BufferedWriter.

如果您正在处理文本数据并且写入操作的次数较少,请使用FileWriter并将其构造函数的附加标志值设置为true 。 如果写入操作的数量很多,则应使用BufferedWriter 。

To append binary or raw stream data to an existing file, you should use FileOutputStream.

要将二进制或原始流数据附加到现有文件,应使用FileOutputStream。

Java使用FileWriter附加到文件 (Java append to file using FileWriter)

Here is the short program to append to file in java using FileWriter. We will look into a complete Java append to file example program later on.

这是使用FileWriter附加到java中文件的简短程序。 稍后,我们将研究完整的Java追加到文件示例程序。

File file = new File("append.txt");
FileWriter fr = new FileWriter(file, true);
fr.write("data");
fr.close();

Java使用BufferedWriter将内容追加到现有文件 (Java append content to existing file using BufferedWriter)

File file = new File("append.txt");
FileWriter fr = new FileWriter(file, true);
BufferedWriter br = new BufferedWriter(fr);
br.write("data");br.close();
fr.close();

使用PrintWriter将文本追加到Java中的文件 (Append text to file in java using PrintWriter)

We can also use PrintWriter to append to file in java.

我们还可以使用PrintWriter附加到java中的文件。

File file = new File("append.txt");
FileWriter fr = new FileWriter(file, true);
BufferedWriter br = new BufferedWriter(fr);
PrintWriter pr = new PrintWriter(br);
pr.println("data");
pr.close();
br.close();
fr.close();

使用FileOutputStream附加到Java中的文件 (Append to file in java using FileOutputStream)

You should use FileOutputStream to append data to file when it’s raw data, binary data, images, videos etc.

当数据是原始数据,二进制数据,图像,视频等时,应使用FileOutputStream将数据追加到文件中。

OutputStream os = new FileOutputStream(new File("append.txt"), true);
os.write("data".getBytes(), 0, "data".length());
os.close();

Java附加到文件示例 (Java append to file example)

Here is the final java append to file program showing all the different options we discussed above.

这是文件程序的最后java追加,显示了我们上面讨论的所有不同选项。

package com.journaldev.files;import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;public class JavaAppendToFile {/*** Java append to file example* * @param args*/public static void main(String[] args) {String filePath = "/Users/pankaj/Downloads/append.txt";String appendText = "This String will be appended to the file, Byte=0x0A 0xFF";appendUsingFileWriter(filePath, appendText);appendUsingBufferedWriter(filePath, appendText, 2);appendUsingPrintWriter(filePath, appendText);appendUsingFileOutputStream(filePath, appendText);}private static void appendUsingPrintWriter(String filePath, String text) {File file = new File(filePath);FileWriter fr = null;BufferedWriter br = null;PrintWriter pr = null;try {// to append to file, you need to initialize FileWriter using below constructorfr = new FileWriter(file, true);br = new BufferedWriter(fr);pr = new PrintWriter(br);pr.println(text);} catch (IOException e) {e.printStackTrace();} finally {try {pr.close();br.close();fr.close();} catch (IOException e) {e.printStackTrace();}}}/*** Use Stream for java append to file when you are dealing with raw data, binary* data* * @param data*/private static void appendUsingFileOutputStream(String fileName, String data) {OutputStream os = null;try {// below true flag tells OutputStream to appendos = new FileOutputStream(new File(fileName), true);os.write(data.getBytes(), 0, data.length());} catch (IOException e) {e.printStackTrace();} finally {try {os.close();} catch (IOException e) {e.printStackTrace();}}}/*** Use BufferedWriter when number of write operations are more* * @param filePath* @param text* @param noOfLines*/private static void appendUsingBufferedWriter(String filePath, String text, int noOfLines) {File file = new File(filePath);FileWriter fr = null;BufferedWriter br = null;try {// to append to file, you need to initialize FileWriter using below constructorfr = new FileWriter(file, true);br = new BufferedWriter(fr);for (int i = 0; i < noOfLines; i++) {br.newLine();// you can use write or append methodbr.write(text);}} catch (IOException e) {e.printStackTrace();} finally {try {br.close();fr.close();} catch (IOException e) {e.printStackTrace();}}}/*** Use FileWriter when number of write operations are less* * @param filePath* @param text* @param noOfLines*/private static void appendUsingFileWriter(String filePath, String text) {File file = new File(filePath);FileWriter fr = null;try {// Below constructor argument decides whether to append or overridefr = new FileWriter(file, true);fr.write(text);} catch (IOException e) {e.printStackTrace();} finally {try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}

That’s all for append to file in java program.

这就是将所有内容追加到java程序中的文件。

GitHub Repository.GitHub存储库中签出更多Java IO示例。

References:

参考文献:

  • PrintWriter API DocPrintWriter API文档
  • FileWriter API DocFileWriter API文档
  • BufferedWriter API DocBufferedWriter API文档
  • FileOutputStream API DocFileOutputStream API文档

翻译自: https://www.journaldev.com/881/java-append-to-file

java追加到文件末尾

java追加到文件末尾_Java追加到文件相关推荐

  1. java读取修改文件内容_Java对本地文件内容读取、修改、删除的操作

    import org.apache.commons.lang.StringUtils; import java.io.*; import java.util.HashMap; import java. ...

  2. java文件递归_java递归处理文件夹和文件

    import java.io.File; /** * 文件综合使用示例 */ public class FileDelete { public static void main(String[] ar ...

  3. java 获取文件权限_Java中的文件权限,检查权限和更改权限 - Break易站

    Java提供了许多方法调用来检查和更改文件的权限,例如可以将只读文件更改为具有写入权限.当用户想要限制文件允许的操作时,需要更改文件权限.例如,文件权限可以从写入更改为只读,因为用户不再想要编辑文件. ...

  4. java 文件 递归_JAVA实现遍历文件夹下的所有文件(递归调用和非递归调用)

    JAVA 遍历文件夹下的所有文件(递归调用和非递归调用) 1.不使用递归的方法调用. public void traverseFolder1(String path) { int fileNum = ...

  5. java获取文件编码_java如何获取文件编码格式

    1:简单判断是UTF-8或不是UTF-8,因为一般除了UTF-8之外就是GBK,所以就设置默认为GBK. 按照给定的字符集存储文件时,在文件的最开头的三个字节中就有可能存储着编码信息,所以,基本的原理 ...

  6. java 压缩文件夹_java 实现压缩文件(单文件 或 文件夹)

    接着上篇了解一下java压缩实现过程,下面的是支持 单文件 或 文件夹 压缩的实现,使用递归. 效果: 代码: package com.gx.compress; import java.io.Buff ...

  7. java用NIO实现文件传输_Java Nio 实现文件的传输

    使用Java Nio实现文件的传输 1.ServerSocket.java package ch2; import java.io.File; import java.io.FileNotFoundE ...

  8. java多线程 文件夹_Java多线程遍历文件夹,广度遍历加多线程加深度遍历结合

    复习IO操作,突然想写一个小工具,统计一下电脑里面的Java代码量还有注释率,最开始随手写了一个递归算法,遍历文件夹,比较简单,而且代码层次清晰,相对易于理解,代码如下:(完整代码贴在最后面,前面是功 ...

  9. java 读取大文件内容_java读取大文件

    java一般读取文件时,将文件文内容全部加在到内存,然后读取,但是这种读取方式很明显不适合读取大文件,在进行大文件处理时,考虑到内存有限,采用分次读取的方式. java分次读取文件内容有三种方式, 1 ...

最新文章

  1. 被人举报,经查属实!985高校公告:撤销其博士学位
  2. ISME:基因组和转录组分析深海古菌Thermoprofundales
  3. java 分页组件_java 代码组装的分页组件
  4. 如何自学python基础-零基础如何学习python
  5. TCP通信的客户端代码实现
  6. PHP学习笔记——Php文件引入
  7. 如何创建HTML Mashup并插入到SAP Cloud for Customer标准页面里
  8. 对未来人机融合智能领域的思考
  9. Serverless 架构就不要服务器了?
  10. ELK之收集haproxy日志
  11. 8.11 Matching Networks 匹配网络
  12. python怎么导入sql数据库_如何用Python3写一段将Excel数据导入SQL数据库?
  13. Linux安装中文字体
  14. 霍兰德SI型如何选专业?霍兰德职业兴趣测试
  15. navicat mysql 免安装_mysql免安装版配置+navicat测试
  16. Javassist简介
  17. Java从入门到放弃 --没放弃就写java程序员职业规划
  18. wm8978 控制接口,
  19. 百度关键词排名查询源码_推荐4个Google关键词排名查询工具
  20. 自然语言处理-中文分词相关算法(MM、RMM、BMM、HMM)

热门文章

  1. 关于asp.net上传图片自动生成缩略图
  2. [转载] python跨行 print:多用(),换行符\要小心,少用+或者不用(其它程序代码跨行用\就行,不能用括号)
  3. 机器学习-数据科学库-day1
  4. verilog中的定点数、浮点数、定点小数、定点整数的表示及运算
  5. Signaltap的使用
  6. 2019.01.02 bzoj3513: [MUTC2013]idiots(fft)
  7. TP5 验证-内置规则
  8. mybatis 调用存储过程
  9. KB奇遇记(9):艰难的上线
  10. windows Server 2008+iis 7.5 部署应用程序