001package org.dromara.warm.flow.core.utils;
002
003import java.io.*;
004
005public class AddCopyrightHeader {
006    public static void main(String[] args) throws Exception{
007        //项目的绝对路径,也就是想修改的文件路径
008        String filePath = "/Users/minliuhua/Desktop/mdata/file/IdeaProjects/min/RuoYi-Vue-Warm-Flow/warm-flow";
009        File f = new File(filePath);
010        String content = "/*\n" +
011                " *    Copyright 2024-2025, Warm-Flow (290631660@qq.com).\n" +
012                " *\n" +
013                " *    Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
014                " *    you may not use this file except in compliance with the License.\n" +
015                " *    You may obtain a copy of the License at\n" +
016                " *\n" +
017                " *       https://www.apache.org/licenses/LICENSE-2.0\n" +
018                " *\n" +
019                " *    Unless required by applicable law or agreed to in writing, software\n" +
020                " *    distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
021                " *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
022                " *    See the License for the specific language governing permissions and\n" +
023                " *    limitations under the License.\n" +
024                " */\n";
025        fileTree(f,content);
026    }
027
028    /**
029     * 取出所有的文件及文件夹
030     * @param f 文件夹对象
031     * @throws Exception
032     */
033    public static void fileTree(File f,String content) throws Exception{
034        File [] t = f.listFiles();
035        for (int i = 0; i < t.length; i++) {
036            if(t[i].isDirectory()){
037                fileTree(t[i],content);
038            }else{
039                insert(t[i],content);
040            }
041        }
042    }
043
044    /**
045     * 开始插入内容
046     * @param f 文件对象
047     * @throws IOException
048     */
049    public static void insert(File f,String content) throws IOException{
050        File temp = File.createTempFile("temp", null);
051        if (f.getName().endsWith(".java")) {
052            temp.deleteOnExit();
053            RandomAccessFile raf = new RandomAccessFile(f, "rw");
054            FileOutputStream tempOut = new FileOutputStream(temp);
055            FileInputStream tempInput = new FileInputStream(temp);
056            raf.seek(0);
057            byte[] buf = new byte[64];
058            int hasRead = 0;
059            while ((hasRead = raf.read(buf))>0) {
060                tempOut.write(buf, 0, hasRead);
061            }
062            raf.seek(0);
063
064            raf.write(content.getBytes());
065            while ((hasRead = tempInput.read(buf))>0) {
066                raf.write(buf,0,hasRead);
067            }
068            raf.close();
069            tempOut.close();
070            tempInput.close();
071        }
072
073    }
074}