从 byte[] 创建一个临时 java.io.文件

2022-09-01 18:22:36

我必须使用一个现有的方法,就像哪里有一个.saveAttachment(Attachment attachment)AttachmentFile attribute

我的问题是我正在检索一个,我想使用此方法保存它。我怎么能有一个“本地”只是为了节省?byte[]File

对不起,如果我的问题很愚蠢,我对Java中的文件知之甚少。


答案 1
File tempFile = File.createTempFile(prefix, suffix, null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(byteArray);

查看相关文档:

File.createTempFile(前缀,后缀,目录);


答案 2

从文件读取所有字节或行

Path file = ...;
byte[] fileArray;
fileArray = Files.readAllBytes(file);

将所有字节或行写入文件

Path file = ...;
byte[] buf = ...;
Files.write(file, buf);