Files创建文件

    /**
     * 创建文件
     *
     * @param filePath 文件目录
     * @param fileName 文件名
     * @param content  内容
     * @return
     */
    public Boolean createFile(String filePath, String fileName, String content) {
        try {
            Files.createDirectories(Paths.get(filePath));
            log.info("创建目录成功: filePath = {}", filePath);
        } catch (IOException e) {
            log.error("创建目录失败: filePath = {}", filePath, e);
            return false;
        }
        try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(filePath, fileName))) {
            writer.write(content);
            writer.flush();
            log.info("文件写入成功: filePath = {}, fileName = {}", filePath, fileName);
        } catch (IOException e) {
            log.error("文件写入失败: filePath = {}, content = {}", filePath, content, e);
            return false;
        }
        return true;
    }

 


版权声明:本文为zzz127333092原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。