简体   繁体   English

Java创建临时文件

[英]Java Create Temp File

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? Java中的TempFile的最大名称长度是TempFileMaximumFilesize取决于我们提到要创建的temp目录或基于其他Java的机器?

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. 什么时候调用deleteOnExit()方法-但是此方法的用途是什么,因为它在JVM关闭时被调用。但是在生产环境中,服务器将运行24 * 7,因此将连续创建文件并将其由于内存而在我们创建文件的服务器上出现问题。

To autoclean temp-files older (modified) than XX seconds... 自动清除(修改)超过XX秒的临时文件...

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

}

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? Java中TempFile的最大名称长度是多少,而在我们提到要创建的temp目录或其他基于Java的计算机上,MaximumFilesize正在扩展?

 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 因此文件名可以是任何随机的long带前缀后缀的文件

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 什么时候调用deleteOnExit()方法-但是此方法的用途是什么,因为它在JVM关闭时被调用。但是在生产环境中,基于服务器的服务器将运行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 例如,当您启动eclipse时,您将看到创建的.lock文件来锁定工作空间,当您的eclipse存在时,它将被删除。

Java中的最大文件大小限制为Long.MAX_VALUE,但是.....,文件名长度受基础文件系统的限制....像EXT4 (Linux)或NTFS (Windows)

String tmpDir = System.getProperty("java.io.tmpdir"); 字符串tmpDir = System.getProperty(“ java.io.tmpdir”);
File file=new File(tmpDir+"\\"+fileName+".tmp"); File file = new File(tmpDir +“ \\” + fileName +“。tmp”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM