Java implements appending content to the file (writing the content to the file)

  In Java projects, text files are sometimes used to record the execution results of some tasks, and then the task execution result value in the file is obtained to determine whether the task has been successfully completed. At this time, it is necessary to implement appending content to the file, that is, writing the content into the file. The implementation code is shown below.

package com.test.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

@Slf4j
public class WriteFile {
    
    

    /**
     * 向文件追加内容
     *
     * @param content  写入的内容
     * @param fileName 文件
     */
    public static void writeFile(String content, String fileName) {
    
    

        // 在文件夹目录下新建文件
        File file = new File(fileName);

        FileOutputStream fos = null;
        OutputStreamWriter osw = null;

        try {
    
    
            if (!file.exists()) {
    
    
                boolean hasFile = file.createNewFile();
                if (hasFile) {
    
    
                    log.info("file not exists, create new file");
                }
                fos = new FileOutputStream(file);
            } else {
    
    
                fos = new FileOutputStream(file, true);
            }

            osw = new OutputStreamWriter(fos, "utf-8");
            // 写入内容
            osw.write(content);
            // 换行
            osw.write("\r\n");
            log.info("成功向文件 [{}] 写入内容:[{}]", fileName, content);
        } catch (Exception e) {
    
    
            log.info("写入文件发生异常", e);
        } finally {
    
    
            // 关闭流
            try {
    
    
                if (osw != null) {
    
    
                    osw.close();
                }
                if (fos != null) {
    
    
                    fos.close();
                }
            } catch (IOException e) {
    
    
                log.info("关闭流异常", e);
            }
        }
    }

	// 将内容 "testFlag_20210112=1" 追加到文件 "D:\\testConfig.properties"
    public static void main(String[] args) {
    
    
        String content = "testFlag_20210112=1";
        String filePath = "D:\\testConfig.properties";
        writeFile(content, filePath);
    }
}

  Among them, the logging tool needs to introduce the following maven dependencies.

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.6</version>
		</dependency>

Guess you like

Origin blog.csdn.net/piaoranyuji/article/details/112563790