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

发布于 2024-10-18 22:59:01 字数 993 浏览 1 评论 0原文

我很抱歉创建了与现在许多类似的线程,但我主要是想对某些方法有一些了解。

我有一个字符串列表(可能只有 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 个不同的起始数字) 不允许使用这些字符 - I,O 最后两个字母字符子集中永远不会有一个字符然后是一个数字。

I apologize for creating a similar thread to many that are out there now, but I mainly wanted to also get some insight on some methods.

I have a list of Strings (could be just 1 or over a 1000)
Format = XXX-XXXXX-XX where each one is alphanumeric

I am trying to generate a unique string (currently 18 in length but probably could be longer ensuring not to maximize file length or path length) that I could reproduce if I have that same list. Order doesn't matter; although I may be interested if its easier to restrict the order as well.

My current Java code is follows (which failed today, hence why I am here):


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;
}

So obviously just adding them did not work out so well I found out (also think I should take last 18 digits and not first 18)

Are there any good methods out there (possibly CRC related) that would work?

To assist with my key creation:
The first 3 characters are almost always numeric and would probably have many duplicate (out of 100, there may only be 10 different starting numbers)
These characters are not allowed - I,O
There will never be a character then a number in the last two alphachar subset.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨夜星沙 2024-10-25 22:59:01

我会使用系统时间。以下是您在 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();
    }

}

I would use the system time. Here's how you might do it in Java:

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

If you want to add some information about the items and their part numbers, you can, of course!

======== EDIT: "What do I mean by batch object" =========

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();
    }

}
老娘不死你永远是小三 2024-10-25 22:59:01

除了 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;
    }

In addition to GlowCoder's response, I have thought of another "decent one" that would work.

Instead of just adding the list in base 36, I would do two separate things to the same list.

In this case, since there is no way for negative or decimal numbers, adding every number and multiplying every number separately and concatenating these base36 number strings isn't a bad way either.

In my case, I would take the last nine digits of the added number and last nine of the multiplied number. This would eliminate my previous errors and make it quite robust. It obviously is still possible for errors once overflow starts occurring, but could also work in this case. Extending the allowable string length would make it more robust as well.

Sample code:


    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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文