java非覆盖写入文件及在输出文本中换行

1、在文件末尾写入而不是覆盖

在我们使用FileWrite方法写入文件时,会发现原来的内容被覆盖了,怎么才能做到追加而不是覆盖呢?

FileWriter(File file, boolean append) ,看到后面的boolean型参数了吧,把boolean型参数设定为true就是追加了

例如:

public static boolean logfile(String tableName) throws IOException {  
        boolean flag = false;  
        byte[] buff = new byte[]{};  
        if (isRiskChar(tableName)) {  
            String message = "表:"" + tableName +""创建失败:" + "表名中含有无效标识符!" + "
";  
            buff = message.getBytes();  
            FileOutputStream out = new FileOutputStream("src//ErrorLog.txt", true);  
            out.write(buff);  
            flag = true;  
        }  
        return flag;  
    }  

2、如何在文件中令文本换行

  1):BufferedWriter的newline()方法

FileOutputStream fos=new FileOutputStream("c;\11.txt");  
BufferedWriter bw=new BufferedWriter(fos);  
bw.write("你好");  
bw.newline();  
bw.write("java");  
w.newline();   

  2):使用转义字符" "

   

String str="aaa";  
str+="
";  

  

原文地址:https://www.cnblogs.com/mo-xue/p/5779761.html