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--------------------文件不存在");
        }
    }


相关文章
|
Java Spring
idea新建spring boot 项目右键无package及java类的选项
idea新建spring boot 项目右键无package及java类的选项
706 5
|
Java Spring
SpringBoot:第一篇 新建spring boot 应用
SpringBoot:第一篇 新建spring boot 应用
133 0
|
Java Spring
IDEA更改工作空间及新建SpringBoot项目
IDEA更改工作空间及新建SpringBoot项目
IDEA更改工作空间及新建SpringBoot项目
新建springboot
新建springboot
|
Java 应用服务中间件 Maven
在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 ....)
1322 0
在idea中新建完springboot项目的时候遇到问题(右键没有class选择;控制台报错:Could not transfer artifact org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.60 from/to central ....)
|
Java Maven Windows
idea下新建的spring boot项目,无法导入依赖以及idea基础回顾
最近在idea中导入spring boot项目的依赖时,无论怎么也无法导入依赖
2317 0
idea下新建的spring boot项目,无法导入依赖以及idea基础回顾
|
存储 Java Spring
使用IntelliJ Idea新建SpringBoot项目
简单给大家介绍一下我来创建SpringBoot项目使用的工具,本人使用IntelliJ Idea来创建项目,利用其中的Spring Initializr工具来快速创建项目。
3144 0
|
6月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1043 0
|
7月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
791 0

热门文章

最新文章