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
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
常常需要递归的替换文件内容,如最近我想写了个jEdit插件,jEdit提供了一个插件模板,我只要全部替换一下就成了我自己的工程。在linux下,这好办,shell来搞定,但在Windows下或者不熟悉shell就得想点其他办法来办了,EditPlus可以,UE也可以,不过不太方便的是文件名不好替换,以前给自己写过一个小工具来成批改MP3的名字,后来发现用处蛮大,今天又有新要求了,我就写了MiniTool来完成这一 MiniCase。 现在已经更新到5.0
资源推荐
资源详情
资源评论
收起资源包目录
replace.5.0.rar (13个子文件)
replace.5.0
.project 383B
bin
console
rep-linux 1.48MB
rep.exe 1.51MB
graphic
rep-linux 1.48MB
rep.exe 1.51MB
src
com
jonsenelizee
replace
MainFrame.java 16KB
Replacer.java 34KB
FormattingPrintWriter.java 11KB
ReplacerUI.java 2KB
rep.jpx.local~ 5KB
rep.jpx 6KB
.classpath 232B
rep.jpx.local 5KB
共 13 条
- 1
资源评论
- tpfpcwl2014-09-29有异常,运行不起来.
- shadowmeteor2012-04-28还行。。就是有些错误
- qq_187525512015-07-04还行,参考了一下。
- 林静心扉2014-11-09这个一般般,不过还是要谢谢!
- liudongyang2014-04-16这个工具基本上有点异常,运行不起来,哈哈
JohnsonElizeee
- 粉丝: 59
- 资源: 35
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功