package com.gds.copyright;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class copyrightAllFiles {
public copyrightAllFiles() {
}
/**
* 获取文件夹下所有文件
*/
public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
try {
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("文件:");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());
long lastcount = file.lastModified();
String lastdate = long2StringDate(lastcount);
if(checkIsJavaFiles(file.getName()))
{
//写入信息
String content = getCorperationInfo(lastdate,file.getName());
writeLogs(file.getAbsolutePath(),content);
}
} else if (file.isDirectory()) {
System.out.println("文件夹:");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath="
+ readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());
long lastcount = readfile.lastModified();
String lastdate = long2StringDate(lastcount);
if(checkIsJavaFiles(readfile.getName()))
{
//写入信息
String content = getCorperationInfo(lastdate,readfile.getName());
writeLogs(readfile.getAbsolutePath(),content);
}
} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return true;
}
public static void writeLogs(String myFilePath,String fileContent)
{
FileWriter resultFile = null;
try {
resultFile = new FileWriter(myFilePath,true);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
try {
resultFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getCorperationInfo(String lastdate,String filename)
{
byte[] c = new byte[2];
c[0] = 0x0d;
c[1] = 0x0a;
String c_string = new String(c);
StringBuilder sb = new StringBuilder();
sb.append(c_string);
sb.append("/**"+c_string);
sb.append("*@公司名称: XXX"+c_string);
sb.append("*@文件名称: "+filename+c_string);
sb.append("*@Date: "+lastdate+c_string);
sb.append("*@Copyright: 2011-2013 http://www.XXX.com/ Inc. All rights reserved."+c_string);
sb.append("**/"+c_string);
return sb.toString();
}
public static boolean checkIsJavaFiles(String filename)
{
System.out.println(filename);
if(null!=filename)
{
String type = filename.substring(filename.lastIndexOf(".")+1, filename.length());
System.out.println("filetype:"+type);
if("java".equals(type))
{
return true;
}
}
return false;
}
public static String long2StringDate(long Time) {
try {
Date date = new Date(Time);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(date);
} catch (Exception exp) {
System.out.println("date convert error:" + exp);
return "";
}
}
public static void main(String[] args) {
try {
readfile("D:/logs/portal");
// deletefile("D:/file");
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
}
System.out.println("ok");
}
}
代码已完成的情况下,想添加代码备注,只能往代码的最后添加。
java批量添加已完成代码备注
最新推荐文章于 2024-01-04 14:54:08 发布