java数字密码字典生成器
程序员文章站
2022-06-21 14:46:23
...
直接上代码,如有不足请指出
import java.io.*;
/**
* 描述:
*java代码实现的字典生成器,新手写的,有不足之处可以指出
*写个main()调用即可,create方法的参数为文件存储的路径
* @author LCY
* @create 2018-10-29 17:57
*/
public class CreateDictionary {
//该方法的参数为保存文件的路径,例:String path = "H:\\测试用\\字典"
public void create(String path){
FileOutputStream fos = null;
BufferedOutputStream bos = null;
PrintWriter pw = null;
try {
System.out.println("开始执行");
fos = new FileOutputStream( path +"\\数字密码字典.txt");
bos = new BufferedOutputStream( fos );
pw = new PrintWriter( bos );
System.out.println("获取io流成功,字典正在生成中...");
int maxInt = 99999999;//最大范围,默认范围0-99999999
int count = 8;//生成密码的位数,默认8位,例:00000001
for(int i = 0;i<=maxInt;i++){
String tempInt = i+"";
int tempIntLength = tempInt.length();
if(tempIntLength!=count){
StringBuilder sb = new StringBuilder( tempInt );
for (int j = count-tempIntLength; j>=1;j--){
sb.insert( 0,0 );
}
pw.println( sb.toString() );
}else{
pw.println( i );
}
}
System.out.println( "字典创建完成" );
} catch (FileNotFoundException e) {
System.out.println("路径填写有误");
e.printStackTrace();
}finally {
pw.close();
try {
bos.close();
fos.close();
System.out.println("io流已关闭");
} catch (IOException e) {
System.out.println("io流关闭出现异常");
e.printStackTrace();
}
}
}
}