上次学长叫我做个关键字链接的工具。他写软文的时候要用。于是我就吭哧吭哧干到3点。好了。。当然是CLI版的。UI有机会以后再说。用java做的。学长早上使用了下说能不能只有第一个匹配的添加?
哦行~java的api刚好有这个函数Matcher.replaceFirst(); 不用自己写算法了~
整个代码没有经过设计。半夜直接码的。。只有一点OO思想。把关键字和链接包装成一个对象了~实践证明这样设计确实增加了代码的usability,testability,Modifibility。
哈 ~总之UED方面还有很多要改进的地方.总之很搓哈,献丑~写这个完全是锻炼下手感,不然javase方面很久没接触了。
用到了string pattern match FileReader 什么的。
然后不多说了附上我之前写的使用文档:
撰写keylink.txt
这个txt里面是一些关键字 和你所想要添加的链接。
每行一对
关键字和链接之间有一个空格

撰写wenzhang.txt
就是你要添加关键字链接的文章,

keylink.txt 和 wenzhang.txt 要放在同一个文件夹下 。注意名字不能变 软件就是根据名字来识别的。
如果keylink.txt 和 wenzhang.txt这两个文件在“D:\2-seo\test” 文件下
那么
输入的时候就输入“D:\\2-seo\\test\\”
注意要打两个反斜杠!! 最后再加两个反斜杠~!!

最后生成的文章在 out.txt中!!


如果不能运行请先下载JRE1.6 进行安装。
//源代码遵循GPL协议~
package html;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//把关键字和 链接都加在 keylink.txt文件中
//把文章加在wenzhang.txt
public class Html {
public static final String keylinkFile = "keylink.txt";
public static final String contentFile = "wenzhang.txt";
// @param args
public static void main(String[] args) {
// TODO Auto-generated method stub
String cp = "";
System.out.println("请输入keylink.txt以及wenzhang.txt文件所在的目录:");
Scanner sc=new Scanner(System.in);
cp=sc.next();
String path = cp+keylinkFile;
FileReader fr = null;
try {
fr = new FileReader(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(fr);
List List_KeyLinkPair = new ArrayList();
String temp = "";// 用于临时保存每次读取的内容
//处理好 keylink.txt文件了
try {
while ((temp = br.readLine()) != null) {
KeyLinkPair tempKLP = new KeyLinkPair();
tempKLP.setKeyLinkpair(temp);
tempKLP.mamimamihong();
List_KeyLinkPair.add(tempKLP);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//然后就是匹配。
String path1 = cp+contentFile;
FileReader fr1 = null;
try {
fr1 = new FileReader(path1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br1 = new BufferedReader(fr1);
String allContent = "";// 用于临时保存每次读取的内容
String line = "";
//处理好 keylink.txt文件了
try {
while ((line= br1.readLine()) != null) {
allContent += line;
allContent +="\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(allContent);
//得到文件的内容之后 进行替换
Iterator iter = List_KeyLinkPair.iterator();
Pattern pattern ;
Matcher matcher;
String each = "";
// String outputContent = "";
while(iter.hasNext()) {
KeyLinkPair kkk =iter.next();
pattern = Pattern.compile(kkk.getKeyWord());
matcher = pattern.matcher(allContent);
//allContent = matcher.replaceAll(kkk.getHtmlLink());
allContent = matcher.replaceFirst(kkk.getHtmlLink());
}
BufferedWriter buff = null;
String outPath = cp+ "\\" + "out.txt";
try {
buff = new BufferedWriter(new FileWriter(outPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
buff.write(allContent);
buff.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(allContent);
}
}
//自定义的一个键值对
class KeyLinkPair
{
/**
* @return the keyWord
*/
public String getKeyWord() {
return keyWord;
}
/**
* @param keyWord the keyWord to set
*/
public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
}
/**
* @return the link
*/
public String getHtmlLink() {
return htmlLink;
}
/**
* @param link the link to set
*/
public void setHtmlLink(String htmlLink) {
this.htmlLink = htmlLink;
}
/**
* @return the keyLinkpair
*/
public String getKeyLinkpair() {
return KeyLinkpair;
}
/**
* @param keyLinkpair the keyLinkpair to set
*/
public void setKeyLinkpair(String keyLinkpair) {
KeyLinkpair = keyLinkpair;
}
/**
* @return the ubbLink
*/
public String getUbbLink() {
return ubbLink;
}
/**
* @param ubbLink the ubbLink to set
*/
public void setUbbLink(String ubbLink) {
this.ubbLink = ubbLink;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url the url to set
*/
public void setUrl(String url) {
this.url = url;
}
//mamimamihong函数 使单个关键字 链接 对应起来~就是加上标签啦 。
public void mamimamihong() {
String [] kl= KeyLinkpair.split(" ");
keyWord = kl[0];
url = kl[1];
htmlLink = ""+keyWord+"";
ubbLink ="[url="+url+"]"+keyWord+"[/url]";
}
String keyWord;
String url ;
String htmlLink;
String ubbLink;
String KeyLinkpair;
}


额 貌似这个代码高亮插件有问题~~~
@gstarwd, 不支持 以
/*
*
*/这种方式添加注释。。