栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

生成字母序列,如A,AA,AAA

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

生成字母序列,如A,AA,AAA

  • 不知道长度
  • 区分大小写
  1. 新建MAVEN工程

	org.apache.poi
	poi
	3.16
 

	org.apache.poi
	poi-ooxml
	3.16

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.poi.EncrypteddocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class App {
	public static void main(String[] args) throws Exception {
		InputStream is = new FileInputStream("D:\aaa.xlsx");
		byte[] buff = new byte[is.available()]; // 将数据读取缓存, 避免频繁访问磁盘
		is.read(buff);
		is.close();

		int threadTotal = 2; // 线程数量
		int start = 1; // 循环开始位置, 便于中断后继续

		ExecutorService es = Executors.newFixedThreadPool(threadTotal);
		for (int i = 0; i < threadTotal; i++) {
			es.execute(new MyTask(start, threadTotal, i, buff));
		}
	}
}


class MyTask implements Runnable {
	static String result = null; // 结果
	byte[] buf = null; // 数据缓存
	int start = 0; // 开始位置
	int step = 0; // 总的线程数量
	int mark = 0; // 运行标记, 用于分配任务

	
	public MyTask(int start, int threadTotal, int mark, byte[] buf) {
		this.start = start + mark;
		this.mark = mark;
		this.step = threadTotal;
		this.buf = buf;
	}

	
	public String mm(long i) {
		if (i < 52) {
			if (i < 26)
				return String.valueOf((char) (65 + i));
			else
				return String.valueOf((char) (97 + i - 26));
		} else {
			return mm(i / 52 - 1) + mm(i % 52);
		}
	}

	private boolean work(String pwd) {
		try {
			InputStream is = new ByteArrayInputStream(buf);
			WorkbookFactory.create(is, pwd);
		} catch (EncrypteddocumentException e) {
			return false;
		} catch (IOException e) {
		} catch (InvalidFormatException e) {
		}
		MyTask.result = pwd;
		return true;
	}

	@Override
	public void run() {
		Thread.currentThread().setName("T" + mark);
		int cnt = 0;
		while (MyTask.result == null) {
			if (cnt++ == 100 && this.mark == 0) {
				System.out.println(Thread.currentThread().getName() + " running at " + start + ", " + mm(start));
				cnt = 1;
			}
			if (work(mm(start)))
				break;
			start += step;
		}
		System.err.println(">>>>>>>>>>>>>>>>" + MyTask.result);
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/490005.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号