zoukankan      html  css  js  c++  java
  • 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+="
    ";  
    

      

  • 相关阅读:
    Orchard CMS中如何打包不带源码的模块
    牛X的CSS3
    Docker指令
    Spring Boot 应用 发布到Docker
    Haproxy全透明代理
    TCP/IP协议理解
    ubuntu tomcat 部署java web
    UDP"打洞"原理
    java多线程-线程通信
    window环境下杀死tomcat
  • 原文地址:https://www.cnblogs.com/mo-xue/p/5779761.html
Copyright © 2011-2022 走看看