首页 > 学院 > 开发设计 > 正文

java向文本文件中写入内容或追加新内容

2019-11-17 04:15:19
字体:
来源:转载
供稿:网友

 /**
     * 向文本文件中写入内容或追加新内容,如果append为true则直接追加新内容,<br>
     * 如果append为false则覆盖原来的内容<br>
     *
     * @param path
     * @param content
     * @param append
     */
    public void writeFile(String path, String content, boolean append) {
        File writefile;
        try {
            // 通过这个对象来判断是否向文本文件中追加内容
            // boolean addStr = append;

            writefile = new File(path);

            // 如果文本文件不存在则创建它
            if (writefile.exists() == false) {
                writefile.createNewFile();
                writefile = new File(path); // 重新实例化
            }

            FileOutputStream fw = new FileOutputStream(writefile);
            System.out.PRintln("###content:" + content);
            fw.write(content.getBytes());
            fw.flush();
            fw.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }


    }


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表