用Java归档的byte []

使用Java:


我有一个byte[]代表文件的文件。


如何将此写入文件(即C:\myfile.pdf)


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


青春有我
浏览 712回答 3
3回答

翻阅古今

使用Apache Commons IOFileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)或者,如果您坚持要自己做...try (FileOutputStream fos = new FileOutputStream("pathname")) {   fos.write(myByteArray);   //fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically close the OutputStream}

素胚勾勒不出你

没有任何库:try (FileOutputStream stream = new FileOutputStream(path)) {    stream.write(bytes);}使用Google Guava:Files.write(bytes, new File(path));使用Apache Commons:FileUtils.writeByteArrayToFile(new File(path), bytes);所有这些策略都要求您在某个时刻也捕获IOException。

慕森王

从Java 7开始,您可以使用try-with-resources语句来避免资源泄漏,并使代码更易于阅读。在这里更多。要将您的内容写入byteArray文件,您可以执行以下操作:try (FileOutputStream fos = new FileOutputStream("fullPathToFile")) {    fos.write(byteArray);} catch (IOException ioe) {    ioe.printStackTrace();}
打开App,查看更多内容
随时随地看视频慕课网APP