4

What is the maximum name length of the TempFile in java and MaximumFilesize is depending on the machine where we mention the temp directory to be created or some other java based?

When to call the deleteOnExit() method--- but what is the use of this method because it gets called when the JVM comes down.But in Production based servers will run 24*7.So file will be created continuously and it will be problem for the server where we create file because of the memory.

user987339
  • 10,519
  • 8
  • 40
  • 45
sunleo
  • 10,589
  • 35
  • 116
  • 196

4 Answers4

8

To autoclean temp-files older (modified) than XX seconds...

import java.io.File;
import java.io.IOException;
import java.util.HashSet;

public class FileAutoCleaner {
    final static FileAutoCleaner singleton = new FileAutoCleaner();
    final HashSet<File> bag = new HashSet<File>();

    public static FileAutoCleaner getInstance() {
        return singleton;
    }

    // This create the temp file and add to bag for checking
    public synchronized File createTempFile(String prefix, String suffix) throws IOException {
        File tmp = File.createTempFile(prefix, suffix);
        tmp.deleteOnExit();
        bag.add(tmp);
        return tmp;
    }

    // Periodically call this function to clean old files   
    public synchronized void cleanOldFiles(final int secondsOld) {
        long now = (System.currentTimeMillis() / 1000);
        for (File f : bag) {
            long expired = (f.lastModified() / 1000) + secondsOld;
            if (now >= expired) {
                System.out.println("Deleted file=" + f.getAbsolutePath());
                f.delete();
                bag.remove(f);
            }
        }
    }

    public static void main(String[] args) throws Exception {
        FileAutoCleaner fac = FileAutoCleaner.getInstance();
        System.out.println(System.currentTimeMillis() / 1000);
        fac.createTempFile("deleteme", "tmp");
        for (int i = 0; i < 5; i++) {
            System.out.println(System.currentTimeMillis() / 1000);
            // delete if older than 2 seconds
            fac.cleanOldFiles(2);
            Thread.sleep(1000);
        }
    }

}
ggrandes
  • 2,067
  • 22
  • 16
  • Hey man thanks, your answer really helped me, I updated it using an `Iterator` and posted as answer to another question here: https://stackoverflow.com/a/61509973/10166336 – HazeyAce Apr 29 '20 at 19:23
3

What is the maximum name length of the TempFile in java and MaximumFilesize is depenting on the machine where we mention the temp directory to be created or some other java based?

 1775           static File generateFile(String prefix, String suffix, File dir) {
 1776               long n = random.nextLong();
 1777               if (n == Long.MIN_VALUE) {
 1778                   n = 0;      // corner case
 1779               } else {
 1780                   n = Math.abs(n);
 1781               }
 1782               return new File(dir, prefix + Long.toString(n) + suffix);
 1783           }

so the file name could be any random long with prefix suffix

When to call the deleteOnExit() method--- but what is the use of this method because it gets called when the JVM comes down.But in Production based servers will run 24*7

There are some file thats needs to be created for application life,

For example when you launch eclipse you will see .lock file created to lock the work space it will get deleted when your eclipse exists

jmj
  • 237,923
  • 42
  • 401
  • 438
1

Maximum file sizes in java are limited to Long.MAX_VALUE but.... this, and filename length are limited by the underlying filesystem.... like EXT4 (Linux) or NTFS (Windows)

Burkhard
  • 14,596
  • 22
  • 87
  • 108
ggrandes
  • 2,067
  • 22
  • 16
0

String tmpDir = System.getProperty("java.io.tmpdir");
File file=new File(tmpDir+"\"+fileName+".tmp");

Saurabh
  • 7,525
  • 4
  • 45
  • 46