天天看点

向文件末尾添加字符串

/**
 * 向文件末尾添加字符串
 * jdk 1.7 新增 try - with - resource
 * 无需finally自动释放资源
 * @author rgy
 *
 */
public class AppendStrDemo {

    public void append(String path){
        try(PrintWriter printWriter = new PrintWriter(new BufferedWriter(new FileWriter(path, true), ))){
            printWriter.write("hello world!");
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        AppendStrDemo apdStrDemo = new AppendStrDemo();
        apdStrDemo.append("src/t.txt");
    }
}