FileWriter 详解

FileWrite 简述

  • public class FileWriter extends OutputStreamWriter:用来写入字符文件的便捷类
  • FileWriter 用于写入字符流,要写入原始字节流,请考虑使用 FileOutputStream。
  • 建议使用“org.apache.commons.io.FileUtils”,Apache已经封装的很好了
  • FileWriter 虽然使用方便,但是写文件时是以系统默认的编码格式输出的,因此对于一些不同编码格式容易产生乱码,尤其对于中文字符, 在使用时应格外留意。为了解决这个问题,可以使用OutputStreamWrite。

构造方法摘要


FileWriter file append

  • public FileWriter(File file) throws IOException:根据给定的 File 对象构造一个 FileWriter 对象。 
  • 参数:file - 要写入数据的 File 对象,不存在时会自动创建
  • 抛出: IOException - 如果该文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/6/29 0029.
 * FileWrite测试
 */
public class FileWriteTest {

    @Test
    public void test1() {
        try {
            String message = "你好,中国!";
            if (message != null && !"".equals(message)) {
                /** 采用日期做文件名,日期加时间做消息头*/
                SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                String logName = simpleDateFormat1.format(new Date());
                String messageHead = simpleDateFormat2.format(new Date());

                /**采用相对路径创建日志目录,即会相对整个项目根目录进行创建*/
                File logDir = new File("logs");
                File logFile = new File(logDir, logName + ".log");

                /**根据文件创建对象,true表示写的内容会追加*/
                FileWriter fileWriter = new FileWriter(logFile, true);
                fileWriter.write(messageHead + "" + message + " 1\r\n");
                fileWriter.write(messageHead + "" + message + " 2\r\n");
                fileWriter.flush();
                fileWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

方法摘要


append csq

  • public Writer append(CharSequence csq) throws IOException:将指定字符序列添加到此 writer。 
  • 参数:csq - 要添加的字符串序列。如果 csq 为 null,则向此 writer 添加四个字符 "null"。 
  • 返回:此 writer 
  • 抛出: IOException - 如果发生 I/O 错误
/**
 * 换行添加内容
 */
@Test
public void test2() {
    try {
        String message = "你好,中国!I Love You!";
        if (message != null && !"".equals(message)) {
            /** 采用日期做文件名,日期加时间做消息头*/
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String logName = simpleDateFormat1.format(new Date());
            String messageHead = simpleDateFormat2.format(new Date());

            /**采用相对路径创建日志目录,即会相对整个项目根目录进行创建*/
            File logDir = new File("logs");
            File logFile = new File(logDir, logName + ".log");

            /**根据文件创建对象
             * 没指明append时,则默认为false,即本次写入的会覆盖源文件内容*/
            FileWriter fileWriter = new FileWriter(logFile);
            /** appendwrite效果一致*/
            fileWriter.append(messageHead + "" + message + " 1\r\n");
            fileWriter.write("我是write.... 2\r\n");
            fileWriter.append(messageHead + "" + message + " 3\r\n");
            fileWriter.write("我是write.... 4\r\n");
            fileWriter.flush();
            fileWriter.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}



猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/80853443