Numbering Files

I have a program that writes files to a specific folder.  Each of these files are numbered (so as to have an "idenification" number).  Is there a way to number them out of order?  That is, I have it set up that it is assigned the number = number of files already created plus one.(due to it starts with zero)
Say somewhere along the line some of the files are deleted, is there a way to us a boolean or something in order to seek out the largest number already assigned
and assign the next file the next number? (so no file is assigned the same number).
Bemery05Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

StillUnAwareCommented:
I assume that Your file names have no extension, only filename part which evaluates to some integer:

File dir = new File("directory where all Your files reside");
String[] list = dir.list();
int maxNum = 0;
for(int i = 0; i < list.length; i++) {
    try {
        maxNum = (int tmp = Integer.parseInt(list[i]) > maxNum) ? tmp : maxNum;
    } catch(NumberFormatException nfe) {
    }
}

maxNum will have the greatest number assigned for file name
dberner9Commented:
If you want to use a file extension for the number, instead of

       maxNum = (int tmp = Integer.parseInt(list[i]) > maxNum) ? tmp : maxNum;

use:

Matcher matcher = Pattern.compile(".*\.(\d+)").matcher(list[i]);
matcher.find();
maxNum = (int tmp = Integer.parseInt(matcher.group(1)) > maxNum) ? tmp : maxNum;
dberner9Commented:
File dir = new File("directory where all Your files reside");
String[] list = dir.list();
int maxNum = 0;
Pattern pattern = Pattern.compile(".*\.(\d+)"));
for(int i = 0; i < list.length; i++) {
    try {
        Matcher matcher = pattern.matcher(list[i]);
        matcher.find();
        maxNum = (int tmp = Integer.parseInt(matcher.group(1)) > maxNum) ? tmp : maxNum;
    } catch(NumberFormatException nfe) {
    }
}
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Bemery05Author Commented:
It doesn't like this line:
 maxNum = (int tmp = Integer.parseInt(list[i]) > maxNum) ? tmp : maxNum;
???
zzynxSr. Software engineerCommented:
>> It doesn't like this line:
Replace by
  maxNum = Math.max(Integer.parseInt(list[i]), maxNum);
CEHJCommented:
If you can square it with your other logic, it may be simpler to create another separate file containing the maximum index

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bemery05Author Commented:
That's exactly what I ended up doing,
Thanks
CEHJCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.