注释和取消注释 程序中的log日志
有点简单,但也是原创哦。。亲测有效,期待指正。
更改了log多行的问题。。
例如//Log
Util;
一、注释log
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class CommentLog {
public static String dirpath = "/home/google/com";
// public static String dirpath = "/home/java";
public static String filepath = "/home/java/test1";
public static String newStr = "LogUtil.";
public static String oldStr = "//LogUtil.";
public static boolean judge = false;
public static String str = null;
public static boolean MyPanduan(String str) {
String strTest = str.trim();
if(!strTest.endsWith(";")) {
judge = true;
} else {
judge = false;
}
return judge;
}
public static void writeStrtoFile(StringBuffer sb1, String path) {
try {
String sb = "" + sb1;
FileOutputStream fos = new FileOutputStream(path, false);// true原有续写,false是追加。如果源文件不存在就新建了
fos.write(sb.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void replaceDir(String Mdirpath) {
File file = new File(Mdirpath);
if (file.isDirectory()) {
File[] tempList = file.listFiles();
for (File fi : tempList) {
if (fi.isFile()) {
if (!fi.getAbsoluteFile().equals("LogUtil.java")) {
replaceFile(fi.getAbsolutePath());
}
judge = false;
} else {
replaceDir(fi.getAbsolutePath());
}
}
}
}
public static void replaceFile(String path) {
try {
// read file content from file
StringBuffer sb = new StringBuffer("");
FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
while ((str = br.readLine()) != null) {
if (str.contains(newStr)) {
str = str.replaceAll(newStr, oldStr);
MyPanduan(str);
sb.append(str);
} else {
if (judge == true) {
str = "//".concat(str);
MyPanduan(str);
}
sb.append(str);
}
sb.append("\n");
}
br.close();
reader.close();
if (sb != null) {
writeStrtoFile(sb, path);
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
replaceDir(dirpath);
}
}
二、取消注释
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CancelCommentLog {
public static String dirpath = "/home/google/com";
//public static String dirpath = "/home/java";
public static String filepath = "/home/java/test1";
public static String newStr = "LogUtil";
public static String oldStr = "//LogUtil";
public static boolean judge = false;
public static String str = null;
public static boolean MyPanduan(String str) {
str = str.trim();
if(!str.endsWith(";")){
judge = true;
} else {
judge = false;
}
return judge;
}
public static void writeStrtoFile(StringBuffer sb1, String path) {
try {
String sb = ""+sb1;
FileOutputStream fos = new FileOutputStream(path, false);// true原有续写,false是追加。如果源文件不存在就新建了
fos.write(sb.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void replaceDir(String Mdirpath){
File file = new File(Mdirpath);
if (file.isDirectory()) {
File[] tempList = file.listFiles();
for (File fi : tempList) {
if (fi.isFile()) {
replaceFile(fi.getAbsolutePath());
judge = false;
}
else{
replaceDir(fi.getAbsolutePath());
}
}
}
}
public static void replaceFile(String path){
try {
// read file content from file
StringBuffer sb= new StringBuffer("");
FileReader reader = new FileReader(path);
BufferedReader br = new BufferedReader(reader);
while((str = br.readLine()) != null) {
String regEx=" +//LogUtil";
String regEx1="//LogUtil";
Pattern pat=Pattern.compile(regEx);
Pattern pat1=Pattern.compile(regEx1);
Matcher mat=pat.matcher(str);
Matcher mat1=pat1.matcher(str);
if(mat.find()){
str=mat.replaceAll(newStr);
MyPanduan(str);
sb.append(str);
}
else if(mat1.find()){
str=mat1.replaceAll(newStr);
MyPanduan(str);
sb.append(str);
}else{
if(judge == true){
str =str.replaceAll("//", " ");
MyPanduan(str);
}
sb.append(str);
}
sb.append("\n");
}
br.close();
reader.close();
if(sb!=null){
writeStrtoFile(sb,path);
}
}
catch(FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
replaceDir(dirpath);
}
}
注释和取消注释 程序中的log日志的更多相关文章
- 程序中编写log日志
public string logFile; ; private Stream s = null; StreamWriter sw = null; /// <summary> /// 用l ...
- Eclipse中各种文件的注释与取消注释的快捷键
Eclipse中各种文件的注释与取消注释的快捷键 Java文件: 注释和取消注释的快捷键都是:CTRL + / 或 Shift+Ctrl+C JS文件: 注释和取消注释的快捷键都是:CTRL + / ...
- IDEA XML注释与取消注释快捷键
IntelliJ IDEA和eclipse中编辑Java文件时,注释和取消注释的快捷键都是: "CTRL + / " 编辑xml文件时, 注释:CTRL + SHIFT + / 取 ...
- XML注释与取消注释快捷键
IntelliJ IDEA和eclipse中编辑Java文件时,注释和取消注释的快捷键都是: "CTRL + / " 编辑xml文件时, 注释:CTRL + SHIFT + / 取 ...
- matlab 大块注释和取消注释的快捷键
matlab 大块注释和取消注释的快捷键 注释:Ctrl+R 取消注释:Ctrl +T
- 在C#中winform程序中应用nlog日志工具
在C#中winform程序中应用nlog日志工具,配置文件简单应用. 文件名 nlog.config,请注意修改属性为"始终复制",发布时候容易遇到不存在文件的错误提示. 通过Nu ...
- 如何在xml文件中加注释或取消注释
1)加注释:Ctrl+Shift+/ 2)取消注释:Ctrl+Shift+\ 本文欢迎转载,但请注明作者与出处: 作者:Sophia 出处:http://www.cnblogs.com/Sophia- ...
- Visual Studio 注释与取消注释快捷键
注释: 先CTRL+K,然后CTRL+C 取消注释: 先CTRL+K,然后CTRL+U
- VS注释与取消注释快捷键
最近在使用VS2010开发ASP.Net,突然发现想全部注释时找不到注释的快捷键,网上查了下,原来很简单,只是需要使用组合键. 注释: 先CTRL+K,然后CTRL+C 取消注释: 先C ...
随机推荐
- ES(1): Creat linux VM on Azure
本章记录在ES集群之前的环境准备工作,主要包含的内容如下: 目录: 创建linux虚拟机 启用root用户 创建linux虚拟机 首先创建一个云服务 按向导创建云服务名称,如下 创建虚拟机, 第二步: ...
- java IO切割流合并流
切割流,将一个较大的文件,切割成多个小文件存储
- php webservice实例(转载)
首先大家要简单了解了何谓webservice,接下来就做两个非常简单的例子,webservice还是逃不开server端(服务器端) 与client端(客户端) . 做这个测试之前,要确认你的php配 ...
- bzoj1085 骑士精神
Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2, ...
- httpclient 用户名密码认证实例
import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.com ...
- python selenium-5根据unittest组织测试用例
driver:浏览器driver存放地址 testcase:测试用例目录 report:测试结果保存目录 runtest.py:执行文件 test_search1.py搜索selenium,test_ ...
- Java 集合 线程安全
Java中常用的集合框架中的实现类HashSet.TreeSet.ArrayList.ArrayDeque.LinkedList.HashMap.TreeMap都是线程不安全的,如果多个线程同时访问它 ...
- 第13章 TCP编程(4)_基于自定义协议的多线程模型
7. 基于自定义协议的多线程模型 (1)服务端编程 ①主线程负责调用accept与客户端连接 ②当接受客户端连接后,创建子线程来服务客户端,以处理多客户端的并发访问. ③服务端接到的客户端信息后,回显 ...
- 使用php的mysqli扩展库操作mysql数据库
简单介绍mysqli: 1.mysqli(mysql improve mysql扩展库的增强版) mysql扩展库和mysqli扩展库的区别 1.mysqli的稳定性 安全性 和 执行效率有所提高 ...
- 深度学习中的Normalization模型
Batch Normalization(简称 BN)自从提出之后,因为效果特别好,很快被作为深度学习的标准工具应用在了各种场合.BN 大法虽然好,但是也存在一些局限和问题,诸如当 BatchSize ...