在Java中使用指定名称创建一个临时文件 [英] create a temporary file with a specified name in java

查看:735
本文介绍了在Java中使用指定名称创建一个临时文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Byte []数组,我想将其内容放入一个临时文件中。

I have a Byte[] array that i want to put it's content into a temporary file .

我已经尝试这样做

try {
            tempFile = File.createTempFile("tmp", null);
            FileOutputStream fos = new FileOutputStream(tempFile);
            fos.write(sCourrier.getBody());
        } catch (IOException e) {
            e.printStackTrace();
        }

但我希望我自己指定文件名,而不要由jvm生成

but i want that I specify the filename by myself so not generated by the jvm

推荐答案

您可以直接提供位置和文件名,也可以访问本地文件系统并找到临时目录

You can directly give the location and file name or You can access local filesystem and find the temp directory

 String tempDir=System.getProperty("java.io.tmpdir");

您可以使用temp目录和自定义文件名。

you can use temp directory and your custom file name.

public static void main(String[] args) {
    try {
        String tempDir=System.getProperty("java.io.tmpdir");
        String sCourrier ="sahu";
        File file = new File(tempDir+"newfile.txt");
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(sCourrier.getBytes());
    } catch (IOException e) {
        e.printStackTrace();
    }

这篇关于在Java中使用指定名称创建一个临时文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆