
Combinationsimport java.io.File;
import java.io.Serializable;
public class Combinations implements Serializable{
/**
*
*/
private static final long serialVersionUID = 6509436857839528414L;
private int min;
private int max;
private char[] chars;// 字符数组
private int charLen;// 字符串长度
private String path ;
private String trueStr ;
private IOUtil iou;
public Combinations() {
this.min = 1;
this.max = trueStr.length() ;
this.charLen = trueStr.length();
this.chars = trueStr.toCharArray();
}
public Combinations(String str, int min, int max, String path) {
this.min = min;
this.max = max;
this.trueStr = str ;
this.charLen = str.length();
this.chars = str.toCharArray();
iou = IOUtil.getInstence(path);
}
public void combination() {
if (min > max) {
int t = max;
max = min;
min = t;
}
for (int i = min; i <= max; i++) {
combination(new StringBuffer(""), i);
}
}
public void combination(StringBuffer str, int length) {
if (length < 1) {
System.out.println("长度小于1");
}
if (length == 1) {
for (int i = 0; i < charLen; i++) {
StringBuffer result = new StringBuffer(str);
result.append(chars[i]);
iou.put(result.toString(),
MD5Util.getMD5Str(result.toString()));
}
}
if (length > 1) {
for (int i = 0; i < charLen; i++) {
StringBuffer temp = new StringBuffer(str);
combination(temp.append(chars[i]), length - 1);
}
}
}
/**
*setter getter
*/
public IOUtil getIou() {
return iou;
}
public void setIou(IOUtil iou) {
this.iou = iou;
}
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public char[] getChars() {
return chars;
}
public void setChars(char[] chars) {
this.chars = chars;
}
public int getCharLen() {
return charLen;
}
public void setCharLen(int charLen) {
this.charLen = charLen;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getTrueStr() {
return trueStr;
}
public void setTrueStr(String trueStr) {
this.trueStr = trueStr;
}
public static void main(String[] args) {
String path = "F:"+File.separator+"md5"
+File.separator+"md5.txt";
Combinations ct = new Combinations("123456",1,4,path);
ct.combination() ;
ct.getIou().close() ;
}
}

md5Utilimport java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util {
/**
* MD5 加密
*/
public static String getMD5Str(String str) {
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5");
messageDigest.reset();
messageDigest.update(str.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught!");
System.exit(-1);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] byteArray = messageDigest.digest();
StringBuffer md5StrBuff = new StringBuffer();
for (int i = 0; i < byteArray.length; i++) {
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
md5StrBuff.append("0").append(
Integer.toHexString(0xFF & byteArray[i]));
else
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
}
return md5StrBuff.toString();
}
}

md5Utilimport java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IOUtil implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1611785226109373473L;
/*
* 接收key 和 value,然后写入一个map,计数器count++如果count>30,IOWrite进入flush;
* 判断文件是否大于30MB,则新建另一文件将map清空,将count设置为0;
*/
private String sourcePath;
private String newPath;
private int MAXSIZE = 30000;
private int fileCount = 0; // 文件计数器
private PrintWriter pw = null;
private List<String> list = new ArrayList<String>();
private static IOUtil instance = new IOUtil();
private IOUtil() {
}
public static IOUtil getInstence(String path) {
if (null == instance) {
instance = new IOUtil();
}
instance.setSourcePath(path);
instance.setNewPath(path);
return instance;
}
public void put(String key, String value) {
//判断path中的文件
File file = new File(newPath) ;
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
}
try {
pw = new PrintWriter(new FileWriter(file));
} catch (IOException e) {
System.out.println("找不到文件" + e.getMessage());
}
if (list.size() <= MAXSIZE) {
list.add(key + " " + value);
} else {
Iterator<String> it = list.iterator();
while (it.hasNext()) {
pw.println(it.next());
}
// 刷新
pw.flush();
list.clear();
this.fileCount++;
this.newPath = this.sourcePath.substring(0, this.sourcePath
.lastIndexOf('.'))
+ "_"
+ this.fileCount
+ this.sourcePath.substring(this.sourcePath
.lastIndexOf('.'));
}
}
@SuppressWarnings("unchecked")
public void close() {
Iterator itor = null;
if (list.size() > 0 && this.pw != null) {
itor = list.iterator();
while (itor.hasNext()) {
pw.println(itor.next());
}
pw.close();
}
itor = null;
pw = null;
}
/*******************
* getter setter
*******************/
public String getSourcePath() {
return sourcePath;
}
public void setSourcePath(String sourcePath) {
this.sourcePath = sourcePath;
}
public String getNewPath() {
return newPath;
}
public void setNewPath(String newPath) {
this.newPath = newPath;
}
public PrintWriter getPw() {
return pw;
}
public void setPw(PrintWriter pw) {
this.pw = pw;
}
}