文件写入并改名

public static void writeFile(String fileName, String content){
        BufferedWriter bufferedWriter = null;
        File file = null;
        try {
            file = new File(path + "/" + fileName + ".txt");
            FileWriter fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(content);
            logger.debug(fileName + "write data success!");
        } catch (IOException e) {
            logger.error(fileName + "write data error!");
            e.printStackTrace();
        }finally {
            if (bufferedWriter != null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 创建新文件对象
        File newFile = new File(path + "/" + fileName + ".done");
        boolean isRenamed = file.renameTo(newFile);
        if (!isRenamed){
            logger.error(fileName + ".done built error");
        }
    }

你可能感兴趣的:(服务器,linux,前端)