从字母数字字符串列表中创建唯一的文件名

时间:2011-02-23 23:00:00

标签: uniqueidentifier alphanumeric

我为现在许多人创建类似的线程而道歉,但我主要想了解一些方法。

我有一个字符串列表(可能只有1或超过1000) 格式= XXX-XXXXX-XX,其中每个都是字母数字

我正在尝试生成一个唯一的字符串(当前长度为18,但可能更长,确保不会最大化文件长度或路径长度),如果我有相同的列表,我可以重现。订单无关紧要;虽然我可能会感兴趣,如果它也更容易限制订单。

我当前的Java代码如下(今天失败,因此我在这里):


public String createOutputFileName(ArrayList alInput, EnumFPFunction efpf, boolean pHeaders) {
    /* create file name based on input list */
    String sFileName = "";
    long partNum = 0;

    for (String sGPN : alInput) {
        sGPN = sGPN.replaceAll("-", ""); //remove dashes
        partNum += Long.parseLong(sGPN, 36);    //(base 36)
    }
    sFileName = Long.toString(partNum);
    if (sFileName.length() > 19) {
        sFileName.substring(0, 18); //Max length of 19
    }
    return alInput;
}

所以显然只是添加它们并没有那么好用我发现了(也认为我应该采取最后18位而不是前18位)

那里有什么好的方法(可能与CRC相关)会起作用吗?

协助创建密钥: 前3个字符几乎总是数字,可能有很多重复(100个,可能只有10个不同的起始数字) 这些字符是不允许的 - 我,O 在最后两个alphachar子集中永远不会有一个字符,而是一个数字。

2 个答案:

答案 0 :(得分:1)

我会使用系统时间。以下是您在Java中的使用方法:

public String createOutputFileName() {
    long mills = System.currentTimeMillis();
    long nanos = System.nanoTime();
    return mills + " " + nanos;
}

如果您想添加有关商品及其部件号的一些信息,您当然可以!

========编辑:“批处理对象是什么意思”=========

class Batch {

    ArrayList<Item> itemsToProcess;
    String inputFilename; // input to external process
    boolean processingFinished;

    public Batch(ArrayList<Item> itemsToProcess) {
        this.itemsToProcess = itemsToProcess;
        inputFilename = null;
        processingFinished = false;
    }

    public void processWithExternal() {
        if(inputFilename != null || processingFinished) {
            throw new IllegalStateException("Cannot initiate process more than once!");
        }
        String base = System.currentTimeMillis() + " " + System.nanoTime();
        this.inputFilename = base + "_input";

        writeItemsToFile();

        // however you build your process, do it here
        Process p = new ProcessBuilder("myProcess","myargs", inputFilename);

        p.start();
        p.waitFor();
        processingFinished = true;
    }

    private void writeItemsToFile() {
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(inputFilename)));
        int flushcount = 0;
        for(Item item : itemsToProcess) {
            String output = item.getFileRepresentation();
            out.println(output);
            if(++flushcount % 10 == 0) out.flush();
        }
        out.flush();
        out.close();
    }

}

答案 1 :(得分:0)

除了GlowCoder的回应之外,我还想到了另一个可行的“体面的”。

我不会只在基数36中添加列表,而是将两个单独的内容添加到同一个列表中。

在这种情况下,由于无法使用负数或十进制数,因此添加每个数字并分别乘以每个数字并连接这些base36数字字符串也不是一个坏的方法。

在我的情况下,我会取加数的最后九位数和加数的最后九位数。这将消除我之前的错误并使其非常强大。一旦溢出开始发生,显然仍然可能出现错误,但在这种情况下也可能有效。扩展允许的字符串长度也会使其更加健壮。

示例代码:


    public String createOutputFileName(ArrayList alInput, EnumFPFunction efpf, boolean pHeaders) {
        /* create file name based on input list */
        String sFileName1 = "";
        String sFileName2 = "";

        long partNum1 = 0;  // Starting point for addition
        long partNum2 = 1;  // Starting point for multiplication

        for (String sGPN : alInput) {
            //remove dashes
            sGPN = sGPN.replaceAll("-", "");
            partNum1 += Long.parseLong(sGPN, 36);    //(base 36)
            partNum2 *= Long.parseLong(sGPN, 36);    //(base 36)
        }

        // Initial strings
        sFileName1 = "000000000" + Long.toString(partNum1, 36);   // base 36
        sFileName2 = "000000000" + Long.toString(partNum2, 36);   // base 36

        // Cropped strings
        sFileName1 = sFileName1.substring(sFileName1.length()-9, sFileName1.length());
        sFileName2 = sFileName2.substring(sFileName2.length()-9, sFileName2.length());

        return sFileName1 + sFileName2;
    }