Java 创建临时文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14420431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 16:12:29  来源:igfitidea点击:

Java Create Temp File

java

提问by sunleo

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

TempFilejava中的最大名称长度是多少,MaximumFilesize取决于我们提到要创建的临时目录的机器或其他一些基于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。所以文件将被连续创建,这对于由于内存,我们在其中创建文件的服务器。

采纳答案by Jigar Joshi

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的最大名称长度是多少,MaximumFilesize取决于我们提到要创建的临时目录的机器或其他一些基于java的机器?

 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 longwith 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 examplewhen 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 存在时,它将被删除

回答by ggrandes

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

}

回答by ggrandes

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)

java 中的最大文件大小被限制为 Long.MAX_VALUE 但是.... 这个和文件名长度受到底层文件系统的限制.... 像EXT4(Linux) 或NTFS(Windows)

回答by Saurabh

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

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