与Java:

我有一个字节[],代表一个文件。

我如何写这个文件(即。C: \ myfile.pdf)

我知道它是用InputStream完成的,但我似乎无法解决它。


当前回答

尝试OutputStream或者更具体地说FileOutputStream

其他回答

没有任何库:

try (FileOutputStream stream = new FileOutputStream(path)) {
    stream.write(bytes);
}

谷歌番石榴:

Files.write(bytes, new File(path));

使用Apache Commons:

FileUtils.writeByteArrayToFile(new File(path), bytes);

所有这些策略都要求您在某个时刻捕获IOException。

File f = new File(fileName);    
byte[] fileContent = msg.getByteSequenceContent();    

Path path = Paths.get(f.getAbsolutePath());
try {
    Files.write(path, fileContent);
} catch (IOException ex) {
    Logger.getLogger(Agent2.class.getName()).log(Level.SEVERE, null, ex);
}

我知道它是用InputStream完成的

实际上,你会写入一个文件输出…

使用java.nio.file的另一个解决方案:

byte[] bytes = ...;
Path path = Paths.get("C:\\myfile.pdf");
Files.write(path, bytes);

尝试OutputStream或者更具体地说FileOutputStream