📄 replacebyinput.java
字号:
package com.pengjj.app;
import java.io.File;
import java.math.BigDecimal;
import org.eclipse.swt.widgets.Display;
import com.pengjj.util.FileUtil;
import com.pengjj.util.PengjjLog;
import com.pengjj.util.ReplaceUtil;
public class ReplaceByInput extends PengjjLog{
private int iFileFinish;
private ReplaceByInputWindow replaceByInputWindow;
private int iFileNum;
/**
* @param args
*/
public static void main(String[] args) {
// PENGJJ_TODO Auto-generated method stub
}
public ReplaceByInput(){
}
/**
* 获得输入窗口对象
* @param replaceByInputWindow
*/
public ReplaceByInput(ReplaceByInputWindow replaceByInputWindow){
this.replaceByInputWindow = replaceByInputWindow;
}
/**
* 开始替换:获得相关的处理信息
* @param file 文件名称或者文件夹的名称
* @param sFileType 文件类型
* @param regex 查找的正则表达式
* @param replace 替换为的内容
*/
public void startReplace(File file, String newFileName,String sFileType, String regex, String replace){
//获得相关的信息
FileUtil fileUtil = new FileUtil();
fileUtil.getFileNumber(file, sFileType);
iFileNum = fileUtil.getINumber();
//替换开始
this.findAllFile(file,null,sFileType,regex,replace);
this.setFinish("end");
this.setText("完成");
}
/**
* 查找目录下的所有的文件
* @param file 文件名称或者文件夹的名称
* @param sFileType 文件类型
* @param regex 查找的正则表达式
* @param replace 替换为的内容
*/
public void findAllFile(File file, String newFileName,String sFileType, String regex, String replace) {
if (file.exists()) {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
findAllFile(files[i], newFileName, sFileType, regex, replace);
}
} else if (file.isFile() && FileUtil.validate(file, sFileType)) {
try {
iFileFinish++;
// 如果新文件的路径为空,则默认覆盖以前的文件
if(newFileName==null||newFileName.trim().length()==0){
newFileName = file.getPath();
}
//显示相关的进度信息
this.setProcess(file, iFileFinish);
ReplaceUtil.convertResource(file.getPath(), newFileName,regex, replace);
} catch (Exception e) {
e.printStackTrace();
log.error("读取文件时发生错误,文件的目录为(replaceByResource File have some error):"
+ file.getPath());
log.error(e.getStackTrace());
}
}
}
}
/**
* 设置处理的进度信息
* @param fileName 正在处理的文件路径
* @param iFinishNum 完成的文件数目
*/
private void setProcess(File fileName,int iFinishNum){
String sMessage = "替换正在进行中,这需要花费一些时间,请不要操作相关的文件,替换进行中......\n\r" +
"开始查找文件:" + fileName.getPath()+",完成:" + devide(iFinishNum,iFileNum)
+ "%";
setText(sMessage);
}
/**
* 设置输入窗口的信息:显示处理进度
*/
private void setText(final String str) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
replaceByInputWindow.setText(str);
}
});
}
/**
* 设置完成标志
* @param str
*/
private void setFinish(final String str) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
replaceByInputWindow.setFinish(str);
}
});
}
/**
* 得到百分比
* @param a
* @param b
* @return
*/
private String devide(int a,int b){
String result = "";
BigDecimal bA = new BigDecimal(a);
bA = bA.multiply(new BigDecimal("100")).divide(new BigDecimal(b),5);
bA.setScale(3,5);
result = bA.toString();
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -