Java Temp File Create getTempFileFor(File file)

Here you can find the source of getTempFileFor(File file)

Description

Find a non-existing file in the same directory using the same name as prefix.

License

Apache License

Parameter

Parameter Description
file file used for the name and location (it is not read or written).

Return

a non-existing file in the same directory using the same name as prefix.

Declaration

public static File getTempFileFor(File file) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

public class Main {
    /**/*from  ww  w.  j  a v a2s  . c  om*/
     * Find a non-existing file in the same directory using the same name as prefix.
     * 
     * @param file file used for the name and location (it is not read or written).
     * @return a non-existing file in the same directory using the same name as prefix.
     */
    public static File getTempFileFor(File file) {
        File parent = file.getParentFile();
        String name = file.getName();
        File result;
        int index = 0;
        do {
            result = new File(parent, name + "_" + index++);
        } while (result.exists());
        return result;
    }
}

Related

  1. getTempFile(String fileId)
  2. getTempFile(String filePrefix, boolean deleteOnExit)
  3. getTempFile(String name)
  4. getTempFile(String tmpdir)
  5. getTempFileDir()
  6. getTempFileGivenName(File file)
  7. getTempFileName()
  8. getTempFileName()
  9. getTempFileName(String base, String ext)