注释和取消注释 程序中的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 ...
随机推荐
- arcgis license manager 10.2服务无法启动
(步骤)1. 用cmd切换到 license manager 安装目录,如 C:\Program Files (x86)\ArcGIS\License10.2\bin,输入: Lmgrd -z -c ...
- dede的织梦问答模块也可以支持arclist标签
dedecms织梦问答等模块支持arclist标签,实现随机调用其他栏目文章 就是让模块模板文件支持调用主站的模板,因为调用主站下的/templets/default/模板,也就实现了支持调用所有标签 ...
- redis在PHP中的基本使用案例(觉得比较实用)
源地址 http://www.t086.com/article/4901
- C# 在引用插件中 出现的问题| Csharp cite the plugin problem
背景:使用C#操纵鼠标进行重复性的工作 background: using Csharp to handle the keyboard or mouse to do the repetitive wo ...
- 常用的sql语句(存储过程语法)
1.存储过程语法 ①package create or replace package PKG_RPT_WAREHOUSE is -- Author : -- Created : 2018/9/28 ...
- 学习笔记之Moq
dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...
- iOS whose view is not in the window hierarchy!
解决方法: viewController只Load完毕,没有Appear,此时应该将语句转移到ViewDidAppear方法中
- 小项目,吃货联盟,java初级小项目,源代码
1:项目的实现效果.功能如图所示. 2:项目的源代码如下: import java.util.Scanner; /** * 吃货联盟订餐管理系统 * */ public class OrderingM ...
- Linux下Nagios的安装与配置 及遇到的坑
原文http://www.jianshu.com/p/7bc822fa8278 不愿意看前5.6c部分可以直接跳到最后看命令. 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能 ...
- django (装饰器,母版继承,自定义,request对象,response对象)
1. 装饰器 1. def wrapper(fn): def inner(*args,**kwargs): 执行被装饰函数之前的操作 ret = fn(*args,** ...