【Java工具】在代码头部加版权

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
 
public class Test {
    public static void readFileByLines() throws Exception{
        //项目的绝对路径,也就是想修改的文件路径
        String filePath = "D:\\develop\\workspace\\test\\src\\main\\java\\com";
        File f = new File(filePath);
        String content = "/*\n"+
                 "* @(#)"+f.getName()+"\n"+
                 "*\n"+ 
                 "* Copyright ***版权信息***.\n"+
                 "*/\n";
        fileTree(f,content);
    }
     
    /**
     * 取出所有的文件及文件夹
     * @param f 文件夹对象
     * @throws Exception
     */
    public static void fileTree(File f,String content) throws Exception{
        File [] t = f.listFiles();
        for (int i = 0; i < t.length; i++) {
            if(t[i].isDirectory()){
                fileTree(t[i],content);
            }else{
                insert(t[i],content);
            }
        }
    }
     
    /*public static void main(String[] args) {
        try {
            readFileByLines();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
     
    /**
     * 开始插入内容
     * @param f 文件对象
     * @throws IOException
     */
    public static void insert(File f,String content) throws IOException{
        File temp = File.createTempFile("temp", null);
        temp.deleteOnExit();
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
        FileOutputStream tempOut = new FileOutputStream(temp);
        FileInputStream tempInput = new FileInputStream(temp);
        raf.seek(0);
        byte[] buf = new byte[64];
        int hasRead = 0;
        while ((hasRead = raf.read(buf))>0) {
            tempOut.write(buf, 0, hasRead);
        }
        raf.seek(0);
         
        raf.write(content.getBytes());
        while ((hasRead = tempInput.read(buf))>0) {
            raf.write(buf,0,hasRead);
        }
        raf.close();
        tempOut.close();
        tempInput.close();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值