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

  • 不知道长度
  • 区分大小写
  1. 新建MAVEN工程
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.16</version>
</dependency> 
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.16</version>
</dependency>
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; // 运行标记, 用于分配任务

	/**
	 * 
	 * @param start
	 *            开始位置
	 * @param threadTotal
	 *            总线程数量
	 * @param mark
	 *            当前线程的标记
	 * @param buf
	 *            数据缓存
	 */
	public MyTask(int start, int threadTotal, int mark, byte[] buf) {
		this.start = start + mark;
		this.mark = mark;
		this.step = threadTotal;
		this.buf = buf;
	}

	/**
	 * 依据输入数字生成字符串[A-Za-z]
	 * 
	 * @param i
	 * @return
	 */
	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);
	}
}

上一篇:C# $符号的使用


下一篇:Linux common Commands