spring boot 新建文件路径,写入文件内容

简介: spring boot 新建文件路径,写入文件内容

创建文件路径,写入文件内容,如果当前路径已存在,则替换掉之前文件内容


/**
     * 创建文件路径,写入文件内容
     * dongxinji
     * 2022年9月28日
     *
     * @throws IOException
     */
    private String checkAndCreate(String date) throws IOException {
        log.info(" 创建文件路径,写入文件内容*******" + date);
        String shell = shellInfo(date);
        File file = new File(path + date + fileName);
        if (!file.exists()) {
            String[] strings = {};
            try {
                createShell(path + date + fileName, strings);//生成文件路径
//                writeToFile(file, shellInfo(date));//对应文件写入内容
                writeToFile(file, shell);//对应文件写入内容
            } catch (Exception e) {
                log.error("shell--------------------Exception" + e.getMessage());
                e.printStackTrace();
            }
        } else {
            log.error("shell--------------------File exist");
            writeToFile(file, shell);//写入文件内容
        }
        return date + fileName;
    }
    /**
     * 写入文件
     *
     * @param shell
     * @throws IOException
     */
    public void writeToFile(File f1, String shell) throws IOException {
        OutputStream out = null;
        BufferedWriter bw = null;
        if (f1.exists()) {
            out = new FileOutputStream(f1);
            bw = new BufferedWriter(new OutputStreamWriter(out, "utf-8"));
            bw.write(shell);
            log.info("写入文件内容******" + shell);
            bw.flush();
            bw.close();
            log.info("shell--------------------" + f1 + "文件创建成功");
        } else {
            log.error("shell--------------------文件不存在");
        }
    }


目录
打赏
0
0
0
0
52
分享
相关文章
|
8月前
|
idea新建spring boot 项目右键无package及java类的选项
idea新建spring boot 项目右键无package及java类的选项
316 5
|
10月前
|
SpringBoot:第一篇 新建spring boot 应用
SpringBoot:第一篇 新建spring boot 应用
60 0
在idea中新建完springboot项目的时候遇到问题(右键没有class选择;控制台报错:Could not transfer artifact org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.60 from/to central ....)
在idea中新建完springboot项目的时候遇到问题(右键没有class选择;控制台报错:Could not transfer artifact org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.60 from/to central ....)
1057 0
在idea中新建完springboot项目的时候遇到问题(右键没有class选择;控制台报错:Could not transfer artifact org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.60 from/to central ....)
idea下新建的spring boot项目,无法导入依赖以及idea基础回顾
最近在idea中导入spring boot项目的依赖时,无论怎么也无法导入依赖
1943 0
idea下新建的spring boot项目,无法导入依赖以及idea基础回顾
使用IntelliJ Idea新建SpringBoot项目
简单给大家介绍一下我来创建SpringBoot项目使用的工具,本人使用IntelliJ Idea来创建项目,利用其中的Spring Initializr工具来快速创建项目。
3063 0
基于SpringBoot+Vue实现的留守儿童爱心网站设计与实现(计算机毕设项目实战+源码+文档)
博主是一位全网粉丝超过100万的CSDN特邀作者、博客专家,专注于Java、Python、PHP等技术领域。提供SpringBoot、Vue、HTML、Uniapp、PHP、Python、NodeJS、爬虫、数据可视化等技术服务,涵盖免费选题、功能设计、开题报告、论文辅导、答辩PPT等。系统采用SpringBoot后端框架和Vue前端框架,确保高效开发与良好用户体验。所有代码由博主亲自开发,并提供全程录音录屏讲解服务,保障学习效果。欢迎点赞、收藏、关注、评论,获取更多精品案例源码。
基于SpringBoot+Vue实现的家政服务管理平台设计与实现(计算机毕设项目实战+源码+文档)
面向大学生毕业选题、开题、任务书、程序设计开发、论文辅导提供一站式服务。主要服务:程序设计开发、代码修改、成品部署、支持定制、论文辅导,助力毕设!
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等