基于文件,对文件内容进行增删该查
package myReplace;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author jpsoft mengzhijun
*
*/
public class DealWithFilesBase2 {//便捷式一站处理xml,query.jsp,update.jsp文件的下划线问题,支持处理业务扩展,只需将要处理的文件放在桌面生成的ObjectFile文件夹里,然后在桌面生成的result文件夹里寻找修改后的文件即可
private String deskopPath="^c:\\\\Users\\(\S){0,}\\Desktop$";//桌面路径匹配表达式
private String deskopPath2="^c:\\Users\\(\S){0,}\\Desktop$";
private String xmlFIleMatch=".xml$";//匹配.xml文件名
private String queryFileMatch="Query(\w){0,}.jsp$";//匹配Query.jsp文件名
private String updateFileMatch="Update(\w){0,}.jsp$";//匹配Update.jsp文件名
private String saveFileMatch=".txt$";//匹配.txt文件名
private String resultxmlPath="xml";
private String resultQueryPath="queryJsp";
private String resultUpdatePath="updateJsp";
private String objectFilePath="ObjectFile";
private String resultFilePath="result";
private String writePath=resultFilePath;//默认路径为result
private String cHARY=String.valueOf('"');
private String SOLIDSk=writePath;
private String userPath="^c:\\Users";
/**
* 直接获取uers的路径
*/
public File getUserPathFile(File h)
{
File file[]=h.listFiles();
for(File f:file){
if(isMatch(userPath, f.getPath()))
{return f;}}
return null;}
/**
* 快速获取桌面位置
* @return
*/
public String getDeskopLocationQuik()
{
File file=new File("C:");
@SuppressWarnings("static-access")
File []homePath=file.listRoots();
for(File h:homePath) {
if(h.isDirectory()){
File file2=getUserPathFile(h);
return findDeskop2(file2).getAbsolutePath();}}
return null;
}
/**
* 找寻桌面2
* @param file
* @return
*/
public File findDeskop2(File file)
{
File tempFile=file;
if(tempFile.exists()&&tempFile.isDirectory()){
if(tempFile.getName().equals("Desktop")&&isMatch(deskopPath2, tempFile.getAbsolutePath())&&(!tempFile.getPath().equals("C:\Users\Default\Desktop"))&&(!tempFile.getPath().equals("C:\Users\Default\Desktop")))
{return tempFile;}
File []files=tempFile.listFiles();
if(files!=null){
for(File f:files)
{
if(f!=null){
tempFile=findDeskop2(f);
if(tempFile.getName().equals("Desktop")&&isMatch(deskopPath2, tempFile.getAbsolutePath())&&(!tempFile.getPath().equals("C:\Users\Default\Desktop"))&&(!tempFile.getPath().equals("C:\Users\Default\Desktop"))){break;};
}}}}
return tempFile;
}
/**
* 找到电脑桌面的位置
* @return
*/
public String getDeskopLocation()
{
File file=new File("C:");
@SuppressWarnings("static-access")
File []homePath=file.listRoots();
for(File h:homePath) {
if(h.isDirectory()){
return findDeskop(file).getAbsolutePath();}}
return null;
}
/**
* 搜索计算机桌面的位置
* @param file
* @return
*/
public File findDeskop(File file)
{
File tempFile=file;
if(tempFile.exists()&&tempFile.isDirectory()){
if(tempFile.getName().equals("Desktop")&&isMatch(deskopPath, tempFile.getAbsolutePath())&&(!tempFile.getPath().equals("C:\Users\Default\Desktop"))&&(!tempFile.getPath().equals("C:\Users\Default\Desktop")))
{return tempFile;}
File []files=tempFile.listFiles();
if(files!=null){
for(File f:files){
if(f!=null){
tempFile=findDeskop(f);
if(tempFile.getName().equals("Desktop")&&isMatch(deskopPath, tempFile.getAbsolutePath())&&(!tempFile.getPath().equals("C:\Users\Default\Desktop"))&&(!tempFile.getPath().equals("C:\Users\Default\Desktop"))){break;};}}
}}
return tempFile;
}
/**
* 判断是否匹配,处理文件名
* @param regex
* @param objectString
* @return
*/
public boolean isMatch(String regex,String objectString)
{Pattern pattern=Pattern.compile(regex);
if(regex.equals(deskopPath)){pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);}
if(regex.equals(deskopPath2)){pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);}
if(regex.equals(userPath)){pattern=Pattern.compile(regex,Pattern.CASE_INSENSITIVE);}
Matcher matcher=pattern.matcher(objectString);
return matcher.find();
}
/**
* 找到和输出匹配
* @param regex
* @param objectString
* @param isUpdate
* @return
*/
public StringBuffer findMatchAndSubMatch(String regex,String objectString,boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find()){
sb.append(matcher.group());
System.out.println("截取:"+sb.toString());}
return sb;
}
/**
* 增加,在同一行
*@param addString 要增加的字符串
* @param regex 正则表达式
* @param objectString 目标字符串
* @param isUpdate
* @return
*/
public StringBuffer findMatchaddStringeL(String regex,String objectString,String addString, boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find())
{System.out.println(matcher.group());
if(matcher.group().indexOf("$")>-1){
matcher.appendReplacement(sb,"\"+ matcher.group()+" "+addString);
}else{
matcher.appendReplacement(sb, matcher.group()+" "+addString);
System.out.println(sb.toString());}}
matcher.appendTail(sb);
return sb;
}
/**
* 处理增加的字段,不同行增加
* @param addString 要增加的字符串
* @param regex 正则表达式
* @param objectString 目标字符串
* @param isUpdate
* @return
*/
public StringBuffer findMatchaddStringDl(String regex,String objectString,String addString, boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find())
{System.out.println("替换前:"+matcher.group());
if(matcher.group().indexOf("$")>-1) {
matcher.appendReplacement(sb,"\"+ matcher.group());}else{
matcher.appendReplacement(sb, matcher.group());
}
}
matcher.appendTail(sb);
sb.append("
").append(addString).append("
");
System.out.println("替换后"+sb.toString());
return sb;
}
/**
* 找到匹配,并替换全部匹配到的
* @param regex 正则表达式
* @param objectString 目标字符串
* @param updateString 用于替换的字符串
* @param isUpdate
* @return
*/
public StringBuffer findMatchreplaceAllMatch(String regex,String objectString,String updateString, boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find()){
matcher.appendReplacement(sb, updateString);}
System.out.println("替换后"+sb.toString());
matcher.appendTail(sb);
return sb;
}
/**
* 用来替换匹配到的字符串中某部分的值
* @param regex 第一次匹配到的
* @param somerRexgex 匹配到再次匹配的正则表达式
* @param objectString
* @param updateString 用来替换的字符串
* @param isUpdate
* @return
*/
public StringBuffer findMatchreplaceSomeMatch(String regex,String somerRexgex,String objectString,String updateString, boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find()){
String temString=findMatchreplaceAllMatch(somerRexgex, matcher.group(), updateString, isUpdate).toString();
matcher.appendReplacement(sb, temString);}
System.out.println("替换后"+sb.toString());
matcher.appendTail(sb);
return sb;
}
/**
* 找到某个匹配并删除
* @param regex
* @param objectString
* @param isUpdate
* @return
*/
public StringBuffer findMatchdelete(String regex,String objectString,boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find())
{System.out.println("替换前"+matcher.group());
matcher.appendReplacement(sb, "");
System.out.println(sb.toString());
}
matcher.appendTail(sb);
return sb;
}
/**
* 处理匹配,并返回处理后的结果
* @param regex
* @param objectString
* @return
*/
public StringBuffer findMatchGroupe(String regex,String objectString,boolean isUpdate)
{
StringBuffer sb=new StringBuffer();
Pattern pattern=Pattern.compile(regex);
Matcher matcher=pattern.matcher(objectString);
while(matcher.find())
{
if(isUpdate){if(isContainfor_(matcher.group())){matcher.appendReplacement(sb,"
");
}
} catch (IOException e) {
e.printStackTrace();
}
return sBuffer;
}
/**
* 处理queryJsp文件
* @param bReader
* @return
*/
public StringBuffer dealWithQueryJsp(BufferedReader bReader)
{
String tempString="";
StringBuffer sBuffer=new StringBuffer();try {
while((tempString=bReader.readLine())!=null){
tempString=findMatchGroupe("(\{(\w){0,}\S{0,}\})|\{(\w){0,}\S{0,}", tempString,false).toString();
sBuffer.append(tempString).append("
");}
} catch (IOException e) {
e.printStackTrace();
}
return sBuffer;
}
/**
* 处理updateJsp
* @param bReader
* @return
*/
public StringBuffer dealWithUpdateJsp(BufferedReader bReader)
{
String tempString="";
StringBuffer sBuffer=new StringBuffer();
try {
while((tempString=bReader.readLine())!=null){
tempString=findMatchGroupe("name=\S(\w){0,}|id=\S(\w){0,}", tempString,true).toString();
sBuffer.append(tempString).append("
");}
} catch (IOException e) {
e.printStackTrace();
}
return sBuffer;
}
/**
* 执行
*/
public void execute()
{ String deskopPath="";
File file=new File("com.jpsoft.savePath.txt");
if(!file.exists()){
try {
System.out.println("第一次运行,可能需要几分钟,请耐心等待.....");
Long beginLong= new Date().getTime();
file.createNewFile();
String path=getDeskopLocationQuik();
System.out.println("查询桌面路径所花费时间:"+(double)((new Date().getTime()-beginLong)/1000)+"s");
produceResultFiles((new StringBuffer()).append(path),file.getName() );
deskopPath= readFile(file).toString();} catch (IOException e) {
System.out.println("文件创建失败");}}
deskopPath= readFile(file).toString();
File objectFileContents=new File(deskopPath+"\"+objectFilePath);
if(!objectFileContents.exists()){objectFileContents.mkdir();System.out.println("目标文件路径创建成功"+" 路径:"+objectFileContents.getPath());}
File resultFileContents=new File(deskopPath+"\"+resultFilePath);
if(!resultFileContents.exists()){resultFileContents.mkdir();System.out.println("结果文件夹路径创建成功"+" 路径:"+resultFileContents.getPath());}
File xmlFileContent=new File(deskopPath+"\"+resultFilePath+"\"+resultxmlPath);
if(!xmlFileContent.exists()){xmlFileContent.mkdir();System.out.println("xml文件夹路径创建成功"+" 路径:"+xmlFileContent.getPath());}
File queryFileContent=new File(deskopPath+"\"+resultFilePath+"\"+resultQueryPath);
if(!queryFileContent.exists()){queryFileContent.mkdir();System.out.println("queryjsp文件夹路径创建成功"+" 路径:"+queryFileContent.getPath());}
File updateFileContent=new File(deskopPath+"\"+resultFilePath+"\"+resultUpdatePath);
if(!updateFileContent.exists()){updateFileContent.mkdir();System.out.println("updateJsp文件夹路径创建成功"+" 路径:"+updateFileContent.getPath());}
deskopPath= readFile(file).toString();
this.writePath=deskopPath+"\"+resultFilePath;
this.SOLIDSk=this.writePath;
scanFile(deskopPath+"/"+objectFilePath);
System.out.println("运行成功");
}
/**
* @param args
*/
public static void main(String[] args) {
DealWithFilesBase2 dFiles=new DealWithFilesBase2();
dFiles.execute();
}
}