package com.jonsenelizee.replace;
import java.io.*;
import java.util.ArrayList;
public class Replacer extends Thread
{
private ArrayList<String> ignored = new ArrayList<String>();
private ArrayList<String> suffixes = new ArrayList<String>();
private BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
private FormattingPrintWriter stdout = new FormattingPrintWriter(System.out, true);
private MainFrame mainFrame = null;
private String dirOrFilePath;
private String optionString;
private String stringFrom;
private String stringTo;
private boolean isWindows;
private int commentLength = 74;
private int fileRenamedCount = 0;
private int fileReplacedCount = 0;
private int folderRenamedCount = 0;
private static String BACKUP_FILE_SUFFIX = "~";
private static String TAB_TO_SPACE = " ";
public static boolean isRunning = false;
private int stringReplacedCount = 0;
private String user_home = "";
public void setMainFrame(MainFrame mainFrame)
{
this.mainFrame = mainFrame;
}
private void loadConfiguration()
{
LineNumberReader lnr = null;
File rc = null;
try
{
user_home = System.getProperty("user.home");
if (System.getProperty("os.name").contains("Windows"))
{
rc = new File(user_home + "/replacerc");
}
else
{
rc = new File(user_home + "/.replacerc");
}
if (rc.exists())
{
lnr = new LineNumberReader(new FileReader(rc));
String line = lnr.readLine();
while (line != null)
{
line = line.replace("\t", "").trim();
if (line.startsWith("#"))
{
line = lnr.readLine();
continue;
}
if (line.startsWith("suffix="))
{
line = line.replace(" ", "").replace("suffix=", "").trim();
String[] xes = line.split(",");
for (String suffix : xes)
{
this.suffixes.add(suffix.toLowerCase());
}
}
if (line.startsWith("commentLength="))
{
line = line.replace(" ", "").replace("commentLength=", "").trim();
this.commentLength = Integer.parseInt(line);
if (this.commentLength < 74)
{
this.commentLength = 74;
}
}
line = lnr.readLine();
}
}
else
{
this.show("INFO [" + rc.getAbsolutePath() + "] doesn't exist.");
this.show("INFO [Please set suffix information this file.]");
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (lnr != null)
{
lnr.close();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args)
{
Replacer app = new Replacer();
app.replace(args);
}
private String[] args;
public void setArgs(String[] args)
{
this.args = args;
}
public void run()
{
isRunning = true;
this.replace(args);
this.mainFrame.textArea.setEditable(false);
isRunning = false;
}
private void replace(String[] args)
{
this.loadConfiguration();
if (!this.isValidArgs(args))
{
return;
}
try
{
this.replace(args[2]);
if (this.optionString.contains("v"))
{
this.show("");
this.show("DIRE [re-named] " + this.folderRenamedCount);
this.show("FILE [re-named] " + this.fileRenamedCount);
this.show("FILE [replaced] " + this.fileReplacedCount);
this.show("ITEM [replaced] " + this.stringReplacedCount);
}
}
catch (FileNotFoundException ex)
{
// is Auto-generated catch block
ex.printStackTrace();
}
catch (IOException ex)
{
// is Auto-generated catch block
ex.printStackTrace();
}
if (this.ignored.size() > 0)
{
this.show("\r\n--------------------------------------------------------------------------------" +
"\r\n ALL THESE FILES ARE IGNORED ACCORDING TO REGISTERED SUFFIX INFORMATION" +
"\r\n--------------------------------------------------------------------------------");
for (String msg : ignored)
{
if (this.optionString.contains("v"))
{
this.show(msg);
}
}
}
}
private String getCMD(String echo)
{
String cmd = "";
if (this.isRunningOnConsole)
{
this.stdout.print(echo);
this.stdout.flush();
try
{
cmd = stdin.readLine();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
else
{
this.mainFrame.echo("\r\n" + echo);
this.isWaitingForCMD = true;
mainFrame.isWaitingForCMD = true;
while (this.isWaitingForCMD)
{
if (mainFrame.cmdReady)
{
cmd = mainFrame.cmd;
this.isWaitingForCMD = false;
mainFrame.isWaitingForCMD = false;
this.mainFrame.cmdReady = false;
}
try
{
Thread.sleep(1);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
}
return cmd.trim();
}
private boolean isBinaryFile(File file)
{
FileInputStream fis = null;
int bytesRead = 0;
boolean isBinary = false;
try
{
fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
bytesRead = fis.read(buffer);
while (bytesRead != -1)
{
for (int i = 0; i < bytesRead; i++)
{
if (buffer[i] >= 0 && buffer[i] <= 7)
{
isBinary = true;
return isBinary;
}
buffer[i] = 0;
}
bytesRead = fis.read(buffer);
}
fis.close();
return isBinary;
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
return isBinary;
}
}
private void replace(String dirOrFilePath) throws IOException
{
if (dirOrFilePath != null)
{
this.dirOrFilePath = dirOrFilePath;
}
File dir = new File(this.dirOrFilePath);
if (!dir.exists())
{
return
![avatar](https://profile-avatar.csdnimg.cn/d511a6fce6f74174823f1afa40c9d01f_jonsenelizee.jpg!1)
JohnsonElizeee
- 粉丝: 58
- 资源: 35
最新资源
- 被忽视的成本:中国城市扩张导致的生态系统服务损失从三耦合的角度来看
- 永磁同步电机PMSM参数辨识的粒子群优化算法实现流程解析与案例探索,基于粒子群优化算法的PMSM参数精准辨识与迭代更新策略,基于粒子群优化算法的永磁同步电机PMSM参数辨识 关键词:永磁同步电机 粒子
- 揭示城市扩张对植被碳的影响封存能力-以长江经济带为例(软件翻译)
- 双馈风电机组与同步发电机组四机两区域Simulink仿真建模及风光储联合调频与多种控制策略结合混合储能技术研究,双馈风电机组与同步发电机组四机两区域Simulink仿真建模及风光储联合调频控制策略,混
- 电气安装工 初级工.pdf
- 清华大学:普通人如何抓住DeepSeek红利
- Swift 编程语言的入门教程,适合零基础或有一定编程经验的读者快速上手
- 清华出品(104页)DeepSeek从入门到精通
- DeepSeek指导手册(24页)
- 研究机翼在不同速度下产生的噪音和性能表现
- JimuFlow RPA工具MacOS版v1.0.0
- MATLAB滚动轴承故障机理建模与仿真分析:基于ODE45的数值计算与多类型故障诊断预测研究,MATLAB轴承动力学模拟:滚动轴承故障机理建模与数值计算,多故障类型模拟及数据分析报告(含故障类型识别与
- 基于改进Relief算法的特征选取与关联向量机在短期负荷预测中的Matlab应用复现,基于改进Relief算法的特征选取与关联向量机在短期负荷预测中的实践(Matlab复现),相关向量机和特征选取技术
- COMSOL模拟技术揭秘:金属合金凝固过程及连铸工艺精确分析-相场流场与温度场的综合运用探究坯壳厚度计算,金属合金凝固与连铸过程数值模拟:相场流场温度场分析下的坯壳厚度计算,comsol数值模拟
- JimuFlow RPA工具Ubuntu版v1.0.0
- 煤层瓦斯渗透扩散与煤体孔隙裂隙二重介质特性研究-基于修正的P-M渗透率模型与气固耦合效应的模拟分析,煤层瓦斯渗透扩散的深部采煤模型研究:建立孔隙裂隙二重介质特性P-M渗透率模型与气固耦合模型的解析
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)