开发者

Creating a unique filename from a list of alphanumeric strings

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 Strin开发者_StackOverflow社区g 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.


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

}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜