博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Java工具】在代码头部加版权
阅读量:6890 次
发布时间:2019-06-27

本文共 2011 字,大约阅读时间需要 6 分钟。

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();    }}

转载于:https://www.cnblogs.com/fyq891014/p/8698049.html

你可能感兴趣的文章
创建windows服务
查看>>
用main函数传参做简单的计算器的代码
查看>>
python中struct.unpack的用法
查看>>
体绘制(Volume Rendering)概述之4:光线投射算法(Ray Casting)实现流程和代码(基于CPU的实现)...
查看>>
Python实践之(七)逻辑回归(Logistic Regression)
查看>>
PAT (Advanced Level) 1107. Social Clusters (30)
查看>>
【开源社群系统研发日记五】ThinkSNS+ 是如何计算字符显示长度的
查看>>
Nodejs日志管理log4js
查看>>
python获取昨日日期
查看>>
海康威视 - 萤石云开放平台 js 版
查看>>
关于分销平台
查看>>
剑指offer---12-**--数值的整数次方
查看>>
PAT - L2-010. 排座位(并查集)
查看>>
Python 学习笔记 - 线程(线程锁,信标,事件和条件)
查看>>
大数据技术服务商个推获4亿人民币D轮融资
查看>>
Git的详细使用教程
查看>>
iOS实现类似苹果手机原生的锁屏界面(数字密码)
查看>>
[vue] 表单输入格式化,中文输入法异常
查看>>
Observer观察者模式与OCP开放-封闭原则
查看>>
如何搭建高级工程师知识框架?推荐两种方式
查看>>