append Number To Filename If Exists - Android File Input Output

Android examples for File Input Output:File Name

Description

append Number To Filename If Exists

Demo Code


//package com.java2s;

import java.io.File;

public class Main {
    public static File appendNumberToFilenameIfExists(String fileName,
            File directory) {//from ww w. ja v a2  s . c o  m
        File newFile = new File(directory, fileName);
        int nextFile = 0;
        while (newFile.exists()) {
            nextFile++;
            newFile = new File(directory, fileName + " ("
                    + Integer.toString(nextFile) + ")");
        }
        return newFile;
    }
}

Related Tutorials